Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with assigning Strings to variables

 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why doesnt line 2 give "true"?I mean after line 1 doesn't s3 refer to the same string literal "abc" in String constant pool that s1 is refering to?
Or is there a different mechanism how a new String is created when "+" operator is used.Please explain.
thanks
 
Ravinderjit Singh
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because will create a new String object, means s1 and s3 are referring to two different objects.
 
Ravinderjit Singh
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This Link might help you.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I was trying out your code because I'm puzzled myself.

When I ran your code and the output is

false
true

However, I changed the line 2 to System.out.println(s1.equals(s3)):



Now the output is

true
true

On the other hand, I'm not sure why s1==s4 outputs to true; my understanding is that String s1, s2, s3 and s4 refer to different objects on the heap - then again, I haven't read the chapter on Strings yet...

 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravinderjit Singh wrote:Because will create a new String object, means s1 and s3 are referring to two different objects.

you mean
String s3=s2+"c";

is like

rather than

is it?
thanks
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The string pool mechanism is at work here. To conserve memory, the compiler will
at times, share a string object if sX.equals(sY). Apparently in your case it put "abc" in
the pool and used it again. (Maybe for s1, s3 and s4).)

Jim ... ...
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandra Bachan wrote:Hello,



hello
go to the link given by Ravinderjit
it explains what is special about Strings
thanks for replying
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Hoglund wrote:The string pool mechanism is at work here. To conserve memory, the compiler will
at times, share a string object if sX.equals(sY). Apparently in your case it put "abc" in
the pool and used it again. (Maybe for s1, s3 and s4).)

Jim ... ...

but it seems as if "abc" is not shared here since "s1==s3" turns out to be false.
thanks
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but it seems as if "abc" is not shared here since "s1==s3" turns out to be false.


From reading the link, my understanding is that if a string is concatenated, it will create a new object. However, if you apply intern(), then it looks for pre-existing String object in memory for s3 to point to. The pre-existing object that s3 originally pointed to is now eligible for garbage collection:



Output:

false
true
After Intern Function on s3:
true
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot sandra
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic