This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Equals Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "String Equals " Watch "String Equals " New topic
Author

String Equals

Pinki Roy
Greenhorn

Joined: May 16, 2008
Posts: 20
Hi All,




In the above code I fail to understand why is displaying false.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Pinki Roy wrote:In the above code I fail to understand why is displaying false.

Because str refers to a String object and sb refers to a StringBuffer object and a String is never equal to a StringBuffer


Joanne
Zeeshan Sheikh
Ranch Hand

Joined: Nov 20, 2011
Posts: 143

It will only return true if the given object represents a String equivalent to this string otherwise its going to return false

So if you want the value to be true you need to override toString () method.



StringBuffer is like a String, but can be modified (mutable) while String object can not be modified (immutable).

Hope this helps.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: String Equals
 
Similar Threads
Please help and explain the output of following code.
Why it prints like this here?
Equal method
StringBuffer and String with equal method
StringBuffer