Compact Strings in Java 9 with Examples
Last Updated :
07 Feb, 2020
Prerequisites: String
Compact String is one of the performance enhancements introduced in the JVM as part of JDK 9. Till JDK 8, whenever we create one String object then internally it is represented as char[], which consist the characters of the String object.
What is the need of Compact String?
- Till JDK 8, Java represent String object as char[] because every character in java is of 2 bytes because Java internally uses UTF-16.
- If any String contains a word in the English language then the character can be represented using a single byte only, we don’t need 2 bytes for each character. Many characters require 2 bytes to represent them but most of the characters require only 1 byte, which falls under LATIN-1 character set. So, there is a scope to improve memory consumption and performance.
- Java 9 introduced the concept of compact Strings. The main purpose of the compact string is whenever we create a string object and the characters inside the object can be represented using 1 byte, which is nothing but LATIN-1 representation, then internally java will create one byte[]. In other cases, if any character requires more than 1 byte to represent it then each character is stored using 2 bytes i.e. UTF-16 representation.
- Thats how Java developers changed the internal implementation of String i.e. known as Compact String, which will improve the memory consumption and performance of String.
String class internal implementation before Java 9:
Java 8 or before
import java.io.Serializable;
public final class String
implements Serializable,
Comparable<String>,
CharSequence {
private final char value[];
}
|
Note: In the above program, we can see that before Java 9, Java represent String object as a char[] only. Suppose we create one String object and object contains the characters which can be represented using 1 byte. Instead of representing the object as byte[] it will create char[] only, which will consume more memory.
JDK developers analyzed that most of the strings can be represented only using Latin-1 characters set. A Latin-1 char can be stored in one byte, which is exactly half of the size of char. This will improve the performance of String.
String class internal implementation from Java 9
Java 9 and after
import java.io.Serializable;
public final class String
implements Serializable,
Comparable<String>,
CharSequence {
private final byte [] value;
private final byte coder;
}
|
Note: Now the question is how will it distinguish between the LATIN-1 and UTF-16 representations? Java developers introduced one final byte variable coder that preserves the information about characters representation. The value of coder value can be:
static final byte LATIN1 = 0;
static final byte UTF16 = 1;
Thus, the new String implementation known as Compact String in Java 9 is better than String before Java 9 in terms of performance because Compact String uses approximately the half area as compared with String in the heap from JDK 9.
Let’s see the difference of the memory used by a String object before Java 9 and from Java 9:
public class Geeks {
public static void main(String[] args)
{
String s
= new String( "Geeksforgeeks" );
}
}
|
Key points to note when we are running on Java 8 or earlier:
- Here, we created a String object with 13 characters and characters inside the object can be represented using 1 byte, which is nothing but LATIN-1 representation.
- If we run the above program with JDK version 8 or earlier then As JDK 8 uses UTF-16 as default, Internally String will be represented as char[].
- Here we don’t need char[], we can represent each character with 1 byte only. Instead of creating byte[], char[] will be created and for each character, 2 bytes are assigned in the heap memory. This is nothing but wastage of heap memory.
public class Geeks {
public static void main(String[] args)
{
String s1 = new String( "Geeksforgeeks" );
String s2 = new String( "Geeksforgeeks€" );
}
}
|
Key points to note when we are running on Java 9:
- From Java 9 as per need char[] or byte[] will be created for String objects. Here as we can see we created String object s1 with 13 characters and object s2 with 14 characters.
- Each character present inside object s1 can be represented using 1 byte only. That’s why for object s1, one byte[] will be created.
- Now for s2, we have one additional character apart from the characters present in object s1 i.e. €. We cant represent € character using LATIN-1 character set. Here we need 2 bytes to represent €. That’s why here Java will use UTF-16 to present the characters represent inside s2.
- For object s2, Internally char[] will be created.
- This is how the new String implementation known as Compact String in Java 9 is better than String before Java 9 in terms of memory consumption and performance.
Similar Reads
ByteBuffer compact() method in Java with Examples
The compact() method of java.nio.ByteBuffer class is used to compact the given buffer.The bytes between the buffer's current position and its limit, if any, are copied to the beginning of the buffer. That is, the byte at index p = position() is copied to index zero, the byte at index p + 1 is copied
3 min read
Pattern compile(String) method in Java with Examples
The compile(String) method of the Pattern class in Java is used to create a pattern from the regular expression passed as parameter to method. Whenever you need to match a text against a regular expression pattern more than one time, create a Pattern instance using the Pattern.compile() method.Synta
2 min read
Buffer capacity() method in Java with Examples
The capacity() method of java.nio.Buffer Class is used to return this buffer's capacity. Syntax: public final int capacity() Return Value: The capacity of this buffer Below are the examples to illustrate the capacity() method: Examples 1: // Java program to demonstrate // capacity() method import ja
2 min read
Throwable Class in Java with Examples
Classes and Objects are basic concepts of Object-Oriented Programming which revolve around real-life entities. A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In this article,
7 min read
Object Compression in Java with Examples
Object Compression is the process of reducing the size of the object with the help of various classes and methods. The receiver then retrieves full information by decompressing the object which is compressed by the sender. Take a look at below figure to distinguish the size of the file before compre
4 min read
Abstract Method in Java with Examples
In Java, Sometimes we require just method declaration in super-classes. This can be achieved by specifying the Java abstract type modifier. Abstraction can be achieved using abstract class and abstract methods. In this article, we will learn about Java Abstract Method. Java Abstract MethodThe abstra
6 min read
Collection add() Method in Java with Examples
The add(E element) of java.util.Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false. Syntax: Collection.add(E element) Paramet
4 min read
Java Swing | JList with examples
JList is part of Java Swing package . JList is a component that displays a set of Objects and allows the user to select one or more items . JList inherits JComponent class. JList is a easy way to display an array of Vectors .Constructor for JList are : JList(): creates an empty blank listJList(E [ ]
4 min read
Pattern compile(String,int) method in Java with Examples
The compile(String, int) method of the Pattern class used to create a pattern from the regular expression with the help of flags where both expression and flags are passed as parameters to the method. The Pattern class contains a list of flags (int constants) that can be helpful to make the Pattern
2 min read
Dynamic Class Data Sharing in Java with Example
In Java, a class is a template that defines the properties and behaviors of objects. Objects are instances of a class and can be created using the new keyword. A class typically consists of fields (also known as instance variables) and methods. Fields are variables that store data, and methods are b
3 min read