String Literal Vs String Object in Java
Understanding the difference between String Literals and String Objects in Java plays a very important role. The main difference between String Literal and String Object is listed below:
- String Literal: A sequence of characters inside double quotes is known as a String Literal. String Literals are stored in a special area, and it is known as the String Pool. String Literals are immutable.
- String Object: String Objects are created with the help of the new keyword, and they can be stored anywhere in memory. String Objects are not stored in the String Pool area.
Difference Between String Literals and String Object
Features | String Literal | String Object |
---|---|---|
Memory Location | It is stored in the String Pool area. | It is stored in the heap area. |
Creation | It is created directly, we just need to assign a value to a variable For example: String s = "GeeksforGeeks" | It is created with the help of a new keyword. For example: s = new String ("GeeksforGeeks") |
Immutablitiy | It is immutable, it simply means it can not be changed once created. | It is also immutable but can be modified indirectly with the help of StringBuilder or other methods |
Performance | It is faster. | It is slower. |
Comparison | Two string literals with the same value point to the same object in memory. | Two string objects with the same value may point to different objects in memory. |
Now, we are going to understand how String Literal and String Objects actually works in Java.
String Literals
In Java, we can use String Literals to initialize a string. A sequence of characters enclosed in double quotes is known as a String Literal.
Declaration:
String s = "Hello GeeksforGeeks"
This string is going to be stored in a special area known as String pool area. The JVM checks if the string already exist in the string pool area or not. If it is already present it will not add and if it is not present, it adds the new string to the pool. This helps avoiding the multiple copies of same string in the memory.
String Object
In Java, we can use String Object to initialize a string. With the help of new keyword we can create string object.
Declaration:
String str = new String("GeeksForGeeks")
This is string object. In this method JVM is forced to create a new string reference, even if "GeeksForGeeks" is in the reference pool. Therefore, if we compare performance of string literal and string object, string object will always take more time to execute than string literal because it will construct a new string every time it is executed.
Note: Execution time is compiler dependent. Below is the Java program to compare their performances.
Example:
// Java program to compare performance
// of string literal and string object
class Geeks {
public static void main(String args[])
{
// Initialization time for String
// Literal
long start1 = System.currentTimeMillis();
for (int i = 0; i < 10000; i++)
{
String s1 = "GeeksForGeeks";
String s2 = "Welcome";
}
long end1 = System.currentTimeMillis();
long total_time = end1 - start1;
System.out.println("Time taken to execute"+
" string literal = " + total_time);
// Initialization time for String
// object
long start2 = System.currentTimeMillis();
for (int i = 0; i < 10000; i++)
{
String s3 = new String("GeeksForGeeks");
String s4 = new String("Welcome");
}
long end2 = System.currentTimeMillis();
long total_time1 = end2 - start2;
System.out.println("Time taken to execute"+
" string object=" + total_time1);
}
}
Output
Time taken to execute string literal = 0 Time taken to execute string object=5