Monday, June 16, 2025
HomeJavaDistinction between String literal and New String object in Java

Distinction between String literal and New String object in Java


The String class or java.lang.String is a particular class in Java API and has so many particular behaviors which aren’t apparent to many programmers. With the intention to grasp Java, step one is to grasp the String class, and one technique to discover is checking what sort of String associated questions are requested on Java interviews. Other than standard questions like why String is last or equals vs == operator, one of the vital steadily requested questions is what’s the distinction between String literal and String object in Java

Then again, in the event you create an object utilizing String literal syntax e.g. “Java”, it could return an present object from String pool (a cache of String object in Perm gen house, which is now moved to heap house in latest Java launch), if it already exists. 

In any other case, it is going to create a brand new string object and put it in a string pool for future re-use. In the remainder of this text, why it is among the most essential issues you must keep in mind about String in Java.

What’s String literal and String Pool?

Since String is among the most used sorts in any utility, the Java designer took a step additional to optimize the makes use of of this class. They know that Strings is not going to be going to be low-cost, and that is why they give you an concept to cache all String cases created inside double quotes e.g. “Java”. These double quoted literal is called String literal and the cache which saved these String cases are generally known as String pool

In earlier variations of Java, I feel up-to Java 1.6 String pool is situated within the permgen space of the heap, however in Java 1.7 updates it is moved to the primary heap space. Earlier because it was in PermGen house, it was at all times a danger to create too many String objects, as a result of it’s totally restricted house, default measurement 64 MB, and used to retailer class metadata e.g. .class recordsdata. 

Now as a result of the String pool is moved to a a lot bigger reminiscence house, it is a lot safer. By the way in which, do not misuse reminiscence right here, at all times attempt to decrease short-term String objects e.g. “a”, “b” after which “ab”. At all times use StringBuilder to take care of short-term String objects.

String literal vs New String in Java

Distinction between String literal and String object

At a excessive stage each are String objects, however the primary distinction comes from the purpose that the new() operator at all times creates a brand new String object. Additionally whenever you create String utilizing literal they’re interned. This might be rather more clear whenever you evaluate two String objects created utilizing String literal and new operator, as proven within the beneath instance :

String a = "Java";
String b = "Java";
System.out.println(a == b);  // True

Right here two completely different objects are created and so they have completely different references:

String c = new String("Java");
String d = new String("Java");
System.out.println(c == d);  // False

Equally, whenever you evaluate a string literal with a String object created utilizing new() operator utilizing == operator, it is going to return false, as proven beneath :

String e = "JDK";
String f =  new String("JDK");
System.out.println(e == f);  // False

On the whole, you must use the string literal notation when potential. It’s simpler to learn and it offers the compiler an opportunity to optimize your code. By the way in which, any reply to this query is incomplete till you clarify what’s String interning, so let’s have a look at that within the subsequent part.

String interning utilizing inter() methodology

Java by default does not put all String objects into the String pool, as an alternative, it offers you the pliability to explicitly retailer any arbitrary object within the String pool. You may put any object to the String pool by calling the intern() methodology of java.lang.String class. Although, whenever you create utilizing String literal notation of Java, it routinely calls intern() methodology to place that object into String pool, offered it was not current within the pool already. 

That is one other distinction between string literal and new string as a result of in case of latest, interning does not occur routinely till you name the intern() methodology on that object. Additionally, remember to make use of StringBuffer and StringBuilder for string concatenation, they’ll cut back the quantity

That is all about this query, what’s the distinction between String literal and String objects in Java. At all times keep in mind that literal Strings are returned from the string pool and Java put them within the pool if not saved already. This distinction is most blatant whenever you evaluate two String objects utilizing equality operator (==). 

That is why it is advised to at all times evaluate two String objects utilizing the equals() methodology and by no means evaluate them utilizing the == operator, since you by no means know which one is coming from pool and which one is created utilizing new() operator. 

If you recognize the distinction between string object and string literal, you too can resolve questions from Java written check, which additionally check this idea. It is one thing, each Java programmer ought to know.  of short-term String object in heap house.


Should you like this text and wanting to be taught extra about String sort in Java, try these wonderful articles from the Java67 weblog :
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments