How to Optimize String Creation in Java?
Last Updated :
02 Dec, 2024
In Java, strings are frequently used, and optimizing their creation can help improve performance, especially when creating many strings in a program. In this article, we will explore simple ways to optimize string creation in Java.
Example:
Using the new
keyword creates a new string object. This is straightforward but can be slow if used repeatedly.
Java
// String creation using new keyword
public class StringCreation {
public static void main(String[] args) {
String[] s = new String[50000]; // Array of strings
long startTime = System.currentTimeMillis();
for (int i = 0; i < 50000; i++) {
// Using 'new' to create strings
s[i] = new String("GeeksforGeeks");
}
long endTime = System.currentTimeMillis();
System.out.println("Time taken using 'new': "
+ (endTime - startTime) + " ms");
}
}
OutputTime taken using 'new': 5 ms
Different Ways to Optimize String Creation in Java
1. Using String.intern()
Method
String.intern() method helps by storing only one copy of each string in memory. This is useful if the same string is used multiple times in your program.
Java
// String creation using intern() method
public class StringCreation {
public static void main(String[] args) {
String[] s = new String[50000]; // Array of strings
long startTime = System.currentTimeMillis();
for (int i = 0; i < 50000; i++) {
// Interning the string
s[i] = new String("GeeksforGeeks").intern();
}
long endTime = System.currentTimeMillis();
System.out.println("Time taken using intern(): "
+ (endTime - startTime) + " ms");
}
}
OutputTime taken using intern(): 43 ms
Note: The creation time of objects with an intern() and creation time of objects with 'new' keyword fluctuate near each other's creation time.
2. Using String Literals
A string literal is the most efficient way to create strings. When you use a string literal, Java automatically checks if the string already exists in the memory and reuses it.
Note: It is the fastest way of string creation.
Example:
Java
// String creation using literals
public class StringCreation {
public static void main(String[] args) {
String[] s = new String[50000]; // Array of strings
long startTime = System.currentTimeMillis();
for (int i = 0; i < 50000; i++) {
// Using a string literal
s[i] = "GeeksforGeeks";
}
long endTime = System.currentTimeMillis();
System.out.println("Time taken using string literals: "
+ (endTime - startTime) + " ms");
}
}
OutputTime taken using string literals: 2 ms
Efficiency of All Methods
new
Keyword: Slower and creates a new string object each time.intern()
Method: Reduces memory usage by reusing strings.- String Literals: Fastest and most efficient way to create strings.
Similar Reads
How to Convert Character Array to String in Java? Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character "\0". A character array can be converted to a string and vice versa. In this article, we will discuss how to convert a character array to a string
4 min read
Convert List of Characters to String in Java Given a list of characters. In this article, we will write a Java program to convert the given list to a string. Example of List-to-String ConversionInput : list = {'g', 'e', 'e', 'k', 's'} Output : "geeks" Input : list = {'a', 'b', 'c'} Output : "abc" Strings - Strings in Java are objects that are
4 min read
How to Initialize and Compare Strings in Java? String is one of the most basic data types and it is widely used in storing and manipulating pieces of text. One of the fundamental features of strings in Java is their immutability that is once a string is created, it will never be changed. Immutability creates two general ways of initialization wi
5 min read
Convert Set of String to Array of String in Java Given a Set of Strings, the task is to convert the Set into an Array of Strings in Java. Examples: Input: Set<String>: ["ForGeeks", "A Computer Portal", "Geeks"] Output: String[]: ["ForGeeks", "A Computer Portal", "Geeks"] Input: Set<String>: ["G", "e", "k", "s"] Output: String[]: ["G",
6 min read
Java program to expand a String if range is given? Suppose we have given a string in which some ranges as specified and we have to place the numbers which is between the given range in the specified place as provided and depicted in the illustration below as follows for a better understanding. Illustration: Input : string x = "1-5, 8, 11-14, 18, 20,
6 min read