This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
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
I agree. Here's the link:
subject: Strings
It's not a secret anymore!