aspose file tools*
The moose likes Beginning Java and the fly likes Difference between these two statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between these two statement" Watch "Difference between these two statement" New topic
Author

Difference between these two statement

Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Hello,
What is the difference between
String s ="Hello"
&
String s= new String("Hello")

??

I am asking this question because in some forum i read in 2nd case two objects are created. If that is true , than how ??


Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi,

because Java creates a special pool for String literals there are actually two objects created:

1) "Hello" as a string literal in the pool
2) The String object you created with new and assigned it to s

Marco
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11243
    
  16

Strings in java get special treatment. I suggest you search around on this site for threads that discuss the "String pool".

in BRIEF, the first time any string literal appears in the code, a String object is created in the String pool.

so when you say "new String("Hello")", the literal creates an object in the pool, and the 'new' operator creates the second object in the heap.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Thank You all ...
Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
Could some one also confirm if this is correct please?

String A = "hello";

String B = "hello";

As I understand it Java will create a referance from A to B (pointer) and only store "hello" once with muliple referances and not actually create two new areas of memory that store "hello"

Is that correct?


Dale Carnegie:<br />"Most of the important things in the world have been accomplished<br />by people who have kept on trying when there seemed to be no hope at all."
Gaurav Thukral
Greenhorn

Joined: Mar 20, 2008
Posts: 2
Yes Stuart

Thats correct.

More details at the following link:

http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

No, the compiler will definitely not create a reference from A to B unless you assign B to A.

I think you mean something else. In this case the compiler creates multiple objects and variables. It creates a normal String object for "hello" which resides on the heap as any other object.
Besides because "hello" is known as a String literal at compile time the compiler creates a reference to it in the string literal pool. This is an optimization of the compiler/JVM special to strings.
And finally there will be two reference variables A and B which point to the same String "hello" which on the heap. I think it was this you were talking about. There's only one object on the heap because A and B use the same string literal.

Marco
Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
Thanks guys that clears that up very well
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Note that, although perfectly legal, it is very rarely necessary or appropriate to use new String("something") constructor. If you are indeed a beginner, you are very unlikely to be in one of those rare situations.

For beginners(*), the semantics of new String("something") are of academic interest only, not practical use.


(*) and most of the time for others, too


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Originally posted by Marco Ehrentreich:
Hi,

because Java creates a special pool for String literals there are actually two objects created:

1) "Hello" as a string literal in the pool
2) The String object you created with new and assigned it to s

Marco


I went through http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html

For the statement,
String s= new String("Hello");

I think the above 2 points contradict in the manner that 2 objects are created on heap and for new operator the object of one is returned to s. So, reference for this should be there in String Pool and another object which is on the heap should be lost.

Now I am really confused. I tried searching for previous post but couldn't really resolves about this issue. Please throw some light on only the above statement.
Night King
Greenhorn

Joined: Apr 03, 2008
Posts: 2
thank you..


thank you ...
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Shivit,

don't worry, I think this is more or less confusing to everyone

As I finally understood the string handling in Java the two differences between Strings and "normal" objects are string literals (enclosed in double quotes) and the behavior of the operator "new".

String literals are only evaluated at compile time and for each a reference is placed in the string literal pool. This is only done by the compiler when no variables are involved (e.g. concatenated).

"new" always creates a new string object at runtime regardless of the string literal pool. This is the reason why you end up with TWO objects on the heap when you write

I'm not an expert why strings were implemented this way and in what situations you gain so much advantage of this. Perhaps the best idea is to not use "new" like Peter already said.

Anyone may correct me please if I told something wrong...

Marco
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
Originally posted by Stuart Smith:
Could some one also confirm if this is correct please?

String A = "hello";

String B = "hello";


Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
@Marco Ehrentreich

Thanks that was really confusing in the beginning.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between these two statement