How are strings implemented in Java 9 under the hood? Is each character stored as one or two bytes?
-
7Since Java 9 hasn't been released yet, is this behavior officially finalized?– Daniel PrydenJul 6, 2016 at 4:39
-
2@DanielPryden - Valid point. However, this late in the development cycle for Java 9 it is unlikely that they will reverse this change.– Stephen CSep 20, 2016 at 10:06
-
@DanielPryden (Java 9 developer here) I concur with Stephen C. The compact strings work is integrated, and it's been well tested and benchmarked. It's not coming out.– Stuart MarksSep 25, 2016 at 1:24
-
2@StuartMarks Just to clarify your last sentence of "It's not coming out" you mean it's not coming out of Java-9 or its not coming out with Java-9? It seems like you mean the former because of your first sentence.– A_ArnoldOct 14, 2016 at 16:51
-
@A_Arnold A fair point! I meant, it's not going to be removed from Java 9, so it will be delivered along with Java 9 when the whole thing is released.– Stuart MarksOct 14, 2016 at 21:56
1 Answer
Because most usages of Strings are Latin-1 and only require one byte, Java-9's String will be updated to be implemented under the hood as a byte array with an encoding flag field to note if it is a byte array. If the characters are not Latin-1 and require more than one byte it will be stored as a UTF-16 char array (2 bytes per char) and the flag. See JEP 254 .