Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the difference in String a = "abc" and String a = new String("abc")

 
AvtarSingh Suchariya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sir,


Can Any One tell me that what is the difference between following 2 lines ?



What is the difference above 2 lines ?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the search next time because then you would have found this
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String a = "abc";

May or may not create a new String object.
If a String object with the literal "abc" already exists the reference 'a' will only point to it.
Since String objects are immutable.

Where as,
String a = new String("abc");
will always create a new Sting object.

 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Wouter, I came to know the exact difference between these two type of instantiation..

So, String a="abc";

its a costly one since object always persist in heap... If many string literals are created like this, Program will be running out of memory... Second format is best comparing to first one...

Constant literal table can be cleared only if JVM is terminated...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ram Narayan.M wrote:its a costly one since object always persist in heap... If many string literals are created like this, Program will be running out of memory... Second format is best comparing to first one...

Umm, no. The second format uses the literal too, so how do you figure it's less "expensive"?
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {
public static void main(String... args) {
String str="java";
String str1 = new String("java");
System.out.println(str.equals(str1));
System.out.println(str.hashCode()+"---"+str1.hashCode());
}
}

When i tested with this program, i got the output as

true
3254818---3254818

Two String object is "equal" and their hashcodes are also equal...

There is a fact .. if two objects are equal by equal() method, then their hashcodes are also equal... Implicitly its the only one object referred by the two reference variables... So, str and str1 reference variables refer to the same "java" string literal in the literal pool... right?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does that make the second form more efficient than the first?
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ram Narayan.M wrote:So, str and str1 reference variables refer to the same "java" string literal in the literal pool... right?

To check the reference equality you should have used '==', and obviously it is going to return false as you have used the 'new' keyword.
So, both are not the same references.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ram Narayan.M wrote:So, String a="abc";
its a costly one since object always persist in heap... If many string literals are created like this, Program will be running out of memory... Second format is best comparing to first one....

is a compile time constant and it will be loaded into the object heap as well as referenced from the string literal pool, but the second one will store the string in the object heap again - it wont take the already available reference in the pool,just because you have used 'new'.
So, see for yourself which one seems to be more efficient in terms of memory.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In worst case scenario..

What if there are large number of string literals referred by the String Litreal Pool?... It stays for long time and leads to the heap
memory space unavalability. So when the JVM is terminated, the string literal pool entries will be cleared...

If its String a= new String("abc");

Ok.. each time "abc" string object is created in the heap due to "new" keyword...

When the reference to the "abc" object is not there, it will be marked for removal by GC... But not in the case of compile time String literals..
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah...4 days have passed by! Just forgot this thread. Did some deep googling on Strings.. Now,i'm pretty much clear. Hope this helps you too.
Ram,
See if this is clear.
Anything within a double quotes is a String literal.

would actually create 2 objects & return 1 reference back. Since "hello" is a String literal,the compiler will make a note of it at compile time and will create a String object in the object heap & the reference is stored in the String literal pool when the Class is loaded for the very first time. This is followed by a creation of another new String object with exactly the same content "hello"(because of new String()). The reference of this second newly created String object in the heap is returned back to str.
If the String str goes out of scope or assigned null, it will be garbage collected sometime or the other.
The problem being, the first reference(of the literal) will continue to exist as long as the JVM runs. It will never be unreferenced - the literal pool holds the reference,remember?

So, again I leave it to you to decide which of the 2 is more efficient.

 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Priety sharma. "" String a = "abc";

May or may not create a new String object.
If a String object with the literal "abc" already exists the reference 'a' will only point to it.
Since String objects are immutable. ""

String s1="ABC";
String s2="ABC";
If above case is true,then

s1==s2 should be used to check equality as both variables points to same memory location ,but it will not Work so we have to use "equals" Method. Why this happens??
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Actually
String s1 = "abc";
does not create a new String. The String "abc" is created when the class is loaded, if it does not already exist from somewhere else.

You should only use == for
  • 1: Comparison with null.
  • 2: Comparison of true singletons, i.e. enum elements.
  • 3: When writing equals methods.
  • 4: If you simply want to find out what happens if you use it.
  • String s1 = "abc";
    String s2 = "abc";

    will set both references equal to the already existing String "abc". So both s1.equals(s2) and s1 == s2 will evaluate to true.

    You realise this is a 5‑year‑old thread? You will find all the information you need in the link Wouter Oet posted.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic