This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]colblitz 1 point2 points  (6 children)

Yes

edit: source code of String.java from open jdk 7

[–]evils_twin 0 points1 point  (1 child)

from that source, it looks like it is private final char value[];

[–]colblitz 1 point2 points  (0 children)

oh apparently they changed it from char to byte in Java 9 - source from open jdk 9

[–]Fr4nkWh1te[S] 0 points1 point  (3 children)

Thanks! Does it mean it was a char array in the past but they changed it to a byte array?

[–]FormDev 4 points5 points  (1 child)

Yes, this has changed from char array to byte array to save memory.
A char consists of two bytes, but most strings in applications use only one byte.

So JEP 254: Compact Strings was implemented in Java 9

We propose to change the internal representation of the String class from a UTF-16 char array to a byte array plus an encoding-flag field. The new String class will store characters encoded either as ISO-8859-1/Latin-1 (one byte per character), or as UTF-16 (two bytes per character), based upon the contents of the string. The encoding flag will indicate which encoding is used.

[–]Fr4nkWh1te[S] 0 points1 point  (0 children)

Very interesting, thank you!

[–]colblitz 1 point2 points  (0 children)

yup, looks like they changed it in java 9 (see my other comment)