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 String / StringBuffer and equals 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 "String / StringBuffer and equals" Watch "String / StringBuffer and equals" New topic

String / StringBuffer and equals

Bernd Stransky
Ranch Hand

Joined: Nov 20, 2001
Posts: 47
consider the following code:
String s = new String("abc");
StringBuffer sb = new StringBuffer("abc");
The expression sb.equals(s) is false, but why is s.equals(sb) false?
I know that StringBuffer does not override equals, but String does. So I expected that s.equals(sb) calls the overloaded String method which goes after the contents of sb, not the reference address itself.
Any ideas?
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Equals on StringBuffer does a shallow comparison. (same like ==) Will return true only if the objects are same.


best regards,<br />Steven<br />SCJP, SCEA
Ricardo Cortes
Ranch Hand

Joined: Jan 23, 2002
Posts: 140
Yep. I always tell myself when I see a StringBuffer and I want to compare a String to it's contents, I have to use toString() on the StringBuffer first.

Sun Certified J2EE Architect for the J2EE Platform (Part 1)<br />Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The equals method takes an Object as a paramater (because it's defined in the Object class), but, in general, if you call equals on one object type and pass it another object type, it'll always come back as false.
It just doesn't make sense for two entirely different types of objects to be "equal." However, had you done this:

Now you'd be comparing a String object with a String object, which makes more sense than comparing a String object with a StringBuffer object.
I hope that helps,

SCJP Tipline, etc.
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
Originally posted by Corey McGlone:

It just doesn't make sense for two entirely different types of objects to be "equal." However, had you done this:

Maybe another way to think about it, if it helps to resolve the confusion, is this:
Sun had something in particular in mind when they created the 'equals' method. There is a moral contract it is expected to obey. That contract is "only return true if the implementation of the two objects is equivalent".
That isn't the same as "only return true if the two objects could be thought of has having equivalent values for some particular purpose".
The Sun notion of equals is more stringent, and really better from an OO programming standpoint. It allows you to have a reasonable expectation that you could use X and Y interchangeably in a wide variety of situations, although there are some limitations to that.

Reid - SCJP2 (April 2002)
Bernd Stransky
Ranch Hand

Joined: Nov 20, 2001
Posts: 47
Hello All,
thanks for your feedback, this is very helpful for me.
vinita Kh
Ranch Hand

Joined: Feb 19, 2002
Posts: 49
Rule of thumb:
(StringBuffer).equals(String) OR
both always return false
I agree. Here's the link:
subject: String / StringBuffer and equals
It's not a secret anymore!