0

I am quite familiar with java, but somehow serialization was always beyond my responsibilities.

Java9 introduced compact strings feature, so characters in strings are always backed into byte[], not char[] array. This is quite good from memory perspective, allowing Latin1 strings to occupy (roughly) x2 less memory. Nevertheless, this is kinda unclear for me from serialization perspective. Let me explain:

Let us have a serializable class:

public class Person implements Serializable
{
    public String name;
    public String surname;

    public Person(String name, String surname)
    {
        this.name = name;
        this.surname = surname;
    }
}

Now if we'll serialize the created person into binary (I won't copy-paste the code, but this is how it is usually being done, we'll have either binary file or byte array.

Now, the question: If we'll have a new Person("John", "Doe") and a new Person("Jön", "Döe") being serialized using java9+, will they be correctly deserialized in earlier releases of java (let's say java8)? How could java handle it if can? Thanks.

Netherwire
  • 2,669
  • 3
  • 31
  • 54
  • The in-memory format of a string does not dictate the serialized form of the string. I can't say for sure, but my guess is: Yeah, not a problem. :-) – T.J. Crowder Jun 23 '18 at 16:51
  • According to the specification of the [Java Object Serialization protocol](https://docs.oracle.com/javase/7/docs/platform/serialization/spec/protocol.html#53801), strings are always serialized into UTF-8. This specification has not been changed for Java 9 – Thomas Kläger Jun 23 '18 at 17:42

0 Answers0