aspose file tools*
The moose likes Beginning Java and the fly likes What is the difference in String a = Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the difference in String a = "abc" and String a = new String("abc")" Watch "What is the difference in String a = "abc" and String a = new String("abc")" New topic
Author

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

AvtarSingh Suchariya
Greenhorn

Joined: Mar 18, 2010
Posts: 3
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

Joined: Oct 25, 2008
Posts: 2700

Please use the search next time because then you would have found this


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

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.


Priety.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

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...


SCJP 6 [SCJP - Old is Gold]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

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"?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

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

Joined: Jan 10, 2002
Posts: 61768
    
  67

How does that make the second form more efficient than the first?
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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.


OCPJP 6
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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

Joined: Jul 11, 2010
Posts: 247

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

Joined: Aug 19, 2009
Posts: 276

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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the difference in String a = "abc" and String a = new String("abc")