Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Java 9's new string implementation?

Tags:

How are strings implemented in Java 9 under the hood? Is each character stored as one or two bytes?

like image 352
A_Arnold Avatar asked Jul 05 '16 21:07

A_Arnold


People also ask

What is string new string in Java?

By new keyword : Java String is created by using a keyword “new”. For example: String s=new String(“Welcome”); It creates two objects (in String pool and in heap) and one reference variable where the variable 's' will refer to the object in the heap.

How is Java string implemented?

String concatenation is implemented through the StringBuilder (or StringBuffer ) class and its append method. String conversions are implemented through the method toString , defined by Object and inherited by all classes in Java.

What is introduced in Java 9?

The Java(9) Platform module system: introduced the following features as part of Jigsaw Project: Modular JDK. Modular Java Source Code. Modular Run-time Images.


1 Answers

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 .

like image 126
A_Arnold Avatar answered Sep 21 '22 21:09

A_Arnold