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 Issue with String class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Issue with String class" Watch "Issue with String class" New topic
Author

Issue with String class

kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87

can someone explain the reason for the two different outputs?what difference does s3 have because s2 is final?
thanks.
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Good Question.....!!!

Same Doubt forwarded to others !!!


SCJP 6.0 96%

(Connecting the Dots ....)
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
You got me.

Would really appreciate if someone can draw how this looks in memory, esp when s2 is final vs. non-final.


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

Sandra Bachan wrote:
Would really appreciate if someone can draw how this looks in memory, esp when s2 is final vs. non-final.


It's not a memory issue. It's a compiler issue.

A final string referenced that is assigned during declaration to a string literal is a compile time constant. So... The s2 reference is a compile time constant. The sum of two compile time constants is a compile time constant. Hence, the assignment of the s3 reference is to a string that is a compile time constant, and hence, in the string pool. So... the first comparison is true.

While the s4 reference is pointing to a string that is in the string pool, the concat being done to be assigned the s5 reference is not done at compile time, and is not in the string pool. And hence... the second comparison is false.

Henry




Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87
Thanks a lot Henry

but at runtime isnt it the same String literal "abc" that lies in the String pool that both s5 and s3 are pointing to?
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 613
line no 7 should be +"c" instead of "a", though the output will still be true.

but at run time isn't it the same String literal "abc" that lies in the String pool that both s5 and s3 are pointing to?


I infer if computation has one non final literal, the string pool is not used.

example :- String s5=s4+"c"; will not use string pool and print false, where as String s5="ab"+"c"; will use pool.

-P


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

kevinn lee wrote:
but at runtime isnt it the same String literal "abc" that lies in the String pool that both s5 and s3 are pointing to?


No. As already explained, s5 is calculated at runtime, and is not in the string pool (whereas s3 is merely assigned at runtime and in the string pool).

Henry
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

I may be missing something but did not understand it fully
Henry sir said that the addition of two compile time constants is a compile time constant
here
s2 is a compile time constant and not the literal "C"
then how can s2 + "C" be the compile time constant?
doesn't the addition s2+"C" occur at runtime?
how does s3 become a compile time constant?
s3 = s2 + "C"


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

Prasad Kharkar wrote:
here
s2 is a compile time constant and not the literal "C"
then how can s2 + "C" be the compile time constant?


String literals are, of course, compile time constants.

Prasad Kharkar wrote:
doesn't the addition s2+"C" occur at runtime?


No. The sum of two compile time constants is done at compile time -- which yields another compile time constant.

Prasad Kharkar wrote:
how does s3 become a compile time constant?
s3 = s2 + "C"


s3 is *not* a compile time constant. It is merely a reference that is assigned to an object that is a compile time constant. Hence, it is a reference to an object in the string pool.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with String class
 
Similar Threads
== with Strings is giving a different result
Doubt in Strings
String puzzle
subString
String and String Object Problem