jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Strings" Watch "Strings" New topic


sabah ammar

Joined: Jan 10, 2005
Posts: 23
Hi ,
I am not very sure how do strings perform in memory when == is tested with attributes.
please explain whatever u can . thanks
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The String methods create a new, unique String only when they change the string they are called on.

"String".trim() does not change the string, so you get back a reference to "String" in the string literal pool.

All of your specific examples have this same effect.

== checks for the same object, so comparing two references to the same string in the string literal pool returns true

Mike Gershman
SCJP 1.4, SCWCD in process
Don't get me started about those stupid light bulbs.
subject: Strings
It's not a secret anymore!