File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes immutablity of strings 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 "immutablity of strings" Watch "immutablity of strings" New topic

immutablity of strings

sree visu
Ranch Hand

Joined: Jul 05, 2007
Posts: 133
hi i have a doubt in the following code..I hope the answer is d but they have given it as "A"..please do clarify

Regards<br />Sree Visu<br />SCJP 5.0, SCWCD 5.0, SCJD(Preparing B&S 2.2.2)
Sergey Petunin
Ranch Hand

Joined: Dec 16, 2007
Posts: 44
For all objects, including strings, the "==" operator checks the reference equality, so it returns true if the two variables refer to the same object in memory.

That's why s2 == s3 gives false: the s3 object is interned in the string pool at compile time, the s2 object is created at runtime.

Though s3 == s4 gives true, because due to the compile time optimization they refer to the same object interned in the pool of strings at compile time.
[ December 19, 2007: Message edited by: Serge Petunin ]
I agree. Here's the link:
subject: immutablity of strings
It's not a secret anymore!