File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem with assigning Strings to variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem with assigning Strings to variables" Watch "problem with assigning Strings to variables" New topic
Author

problem with assigning Strings to variables

kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87

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

Joined: Jan 13, 2010
Posts: 70
Because will create a new String object, means s1 and s3 are referring to two different objects.
Ravinderjit Singh
Ranch Hand

Joined: Jan 13, 2010
Posts: 70
This Link might help you.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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...


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87
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

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87
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

Joined: Feb 15, 2010
Posts: 87
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

Joined: Feb 18, 2010
Posts: 434
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

Joined: Feb 15, 2010
Posts: 87
thanks a lot sandra
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with assigning Strings to variables
 
Similar Threads
Issue with String class
String Objects
String problem
== with Strings is giving a different result
String