Difference between String literal and New String object in Java (original) (raw)
The String class or java.lang.String is a special class in Java API and has so many special behaviors which are not obvious to many programmers. In order to master Java, the first step is to master the String class, and one way to explore is checking what kind of String related questions are asked on Java interviews. Apart from usual questions like why String is final or equals vs == operator, one of the most frequently asked questions is what is the difference between String literal and String object in Java.
For example, what is the difference between String object created in the following two expressions:
String strObject = new String("Java");
and
String strLiteral = "Java";
Both expressions give you a String object, but there is a subtle difference between them. When you create a String object using the new() operator, it always creates a new object in heap memory.
On the other hand, if you create an object using String literal syntax e.g. "Java", it may return an existing object from String pool (a cache of String object in Perm gen space, which is now moved to heap space in recent Java release), if it already exists.
Otherwise, it will create a new string object and put it in a string pool for future re-use. In the rest of this article, why it is one of the most important things you should remember about String in Java.
What is String literal and String Pool?
Since String is one of the most used types in any application, the Java designer took a step further to optimize the uses of this class. They know that Strings will not be going to be cheap, and that's why they come up with an idea to cache all String instances created inside double quotes e.g. "Java". These double quoted literal is known as String literal and the cache which stored these String instances are known as String pool.
In earlier versions of Java, I think up-to Java 1.6 String pool is located in the permgen area of the heap, but in Java 1.7 updates it's moved to the main heap area. Earlier since it was in PermGen space, it was always a risk to create too many String objects, because it's very limited space, default size 64 MB, and used to store class metadata e.g. .class files.
Now because the String pool is moved to a much larger memory space, it's much safer. By the way, don't misuse memory here, always try to minimize temporary String objects e.g. "a", "b" and then "ab". Always use StringBuilder to deal with temporary String objects.
Difference between String literal and String object
At a high level both are String objects, but the main difference comes from the point that the new() operator always creates a new String object. Also when you create String using literal they are interned. This will be much more clear when you compare two String objects created using String literal and new operator, as shown in the below example :
String a = "Java"; String b = "Java"; System.out.println(a == b); // True
Here two different objects are created and they have different references:
String c = new String("Java"); String d = new String("Java"); System.out.println(c == d); // False
Similarly, when you compare a string literal with a String object created using new() operator using == operator, it will return false, as shown below :
String e = "JDK"; String f = new String("JDK"); System.out.println(e == f); // False
In general, you should use the string literal notation when possible. It is easier to read and it gives the compiler a chance to optimize your code. By the way, any answer to this question is incomplete until you explain what is String interning, so let's see that in the next section.
String interning using inter() method
Java by default doesn't put all String objects into the String pool, instead, it gives you the flexibility to explicitly store any arbitrary object in the String pool. You can put any object to the String pool by calling the intern() method of java.lang.String class. Though, when you create using String literal notation of Java, it automatically calls intern() method to put that object into String pool, provided it was not present in the pool already.
This is another difference between string literal and new string because in case of new, interning doesn't happen automatically until you call the intern() method on that object. Also, don't forget to use StringBuffer and StringBuilder for string concatenation, they will reduce the number
That's all about this question, what is the difference between String literal and String objects in Java. Always remember that literal Strings are returned from the string pool and Java put them in the pool if not stored already. This difference is most obvious when you compare two String objects using equality operator (==).
That's why it's suggested to always compare two String objects using the equals() method and never compare them using the == operator, because you never know which one is coming from pool and which one is created using new() operator.
If you know the difference between string object and string literal, you can also solve questions from Java written test, which also test this concept. It's something, every Java programmer should know. of temporary String object in heap space.