How to Optimize String Creation in Java?
The new operator instantiates a class by dynamically allocating(i.e, allocation at run time) memory for a new object and returning a reference to that memory. This reference is then stored in the variable. The new operator is also followed by a call to a class constructor, which initializes the new object. As string creation is the process optimally working in O(n) time complexity, where n is the length of the string.
Execution time may vary according to the performance of the computational machine.
Example :
String str = new String("w3wiki");
Time required for creation of String using new:
Java
// String creation using new keyword import java.lang.*; class GFG { public static void main(String[] args) { // Array of string String str[] = new String[ 50000 ]; // Clock starts long startTime = System.currentTimeMillis(); for ( int i = 0 ; i < 50000 ; i++) { // Create new String object and copy str[i] = new String( "w3wiki" ); } // End Clock long endTime = System.currentTimeMillis(); // Print Time System.out.println( "Creation time of String using 'new' keyword : " + (endTime - startTime) + " ms" ); } } |
Creation time of String using 'new' keyword : 12 ms
How to Optimize String Creation?
1. By using String.intern() method:
String.intern() Method for storing only one copy of each distinct String Value, which must be immutable. It can be used to return a string from memory if it is created by a new keyword. It creates an exact copy of heap string object in string constant pool.
Syntax :
public String intern()
Return: Interned string.
Time required for creation of String using intern() method:
Java
// String creation using intern() method import java.lang.*; class GFG { public static void main(String[] args) { // Array of String String str[] = new String[ 50000 ]; // Clock Starts long startTime = System.currentTimeMillis(); for ( int i = 0 ; i < 50000 ; i++) { // Create new String object and copy str[i] = new String( "w3wiki" ); // Use of intern() str[i] = str[i].intern(); } // End Clock long endTime = System.currentTimeMillis(); // Print Time System.out.println( "Creation time of String objects with intern() : " + (endTime - startTime) + " ms" ); } } |
Creation time of String objects with intern() : 20 ms
The creation time of objects with an intern( ) and creation time of objects with ânewâ keyword fluctuate near each otherâs creation time.
2. By using string literals:
A string literal should be enclosed in double-quotes. Whenever it encounters a string literal in the programmerâs code, the compiler creates a String object with its value.
It is the fastest way of string creation.
Example :
String literal = "w3wiki";
Time required for creation of String using literals:
Java
// String creation using literals import java.lang.*; class GFG { public static void main(String[] args) { // Array of String String str[] = new String[ 50000 ]; // Clock starts long startTime = System.currentTimeMillis(); for ( int i = 0 ; i < 50000 ; i++) { str[i] = "w3wiki" ; } // End Clock long endTime = System.currentTimeMillis(); // Print Time System.out.println( "Creation time of String literals : " + (endTime - startTime) + " ms" ); } } |
Creation time of String literals : 9 ms
Contact Us