Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between these two statement

 
Shivit Agarwal
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You all ...
 
Stuart Smith
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Gaurav Thukral
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Stuart

Thats correct.

More details at the following link:

http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys that clears that up very well
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you..
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Marco Ehrentreich

Thanks that was really confusing in the beginning.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic