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 Comparing String with StringBuffer 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 "Comparing String with StringBuffer" Watch "Comparing String with StringBuffer" New topic

Comparing String with StringBuffer

Tausif Khanooni
Ranch Hand

Joined: Nov 14, 2002
Posts: 107
I got caught with this question in a mock exam.
The code is:
String str = "Java";
StringBuffer buffer = new StringBuffer (str);
if (str.equals(buffer))
System.out.println ("Both are equal");
System.out.println ("Both are not equal");
The correct answer is "Both are not equal". but what I thaught was, since .equals() method always checks the contents of the objects, will pass the "if" condition.
can any one help me please?
Thnks & regards

"Walking on water and building IT Architecture from <br />specification are easy if and only if both are frozen"
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 204
Hi Tausif,
The String class overrides the equals() method of the object class. The java doc for equals() method of String class says

public boolean equals(Object anObject) Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Hope this helps you

SCJP 1.4, SCWCD 1.4
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Sometimes it helps to go see the source code of J2SE classes. Below is the code for the equals method of the String class.
As you can see, the method will unconditionally return false if the type of the given argument is not of type String. This makes sense when you come to think about how two objects of different types could ever be equal.
The bottom line is: String and StringBuffer objects are always different as far as equals is concerned.
BUT, the condition would yield true if you would compare str with the content of the StringBuffer and not the StringBuffer itself. To get the content of the StringBuffer just invoke toString() on it.
str.equals(buffer.toString()) would yield true, and thus, satisfy the condition.

[ March 04, 2003: Message edited by: Valentin Crettaz ]

[Blog] [Blogroll] [My Reviews] My Linked In
Tausif Khanooni
Ranch Hand

Joined: Nov 14, 2002
Posts: 107
Thanks Valentin,
I m cool now
Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109

This will produce the sentence: Both are equal
The method equals() is overridden. It will check if the object that buffer.toString() returns is an instanceof String. If this is the case, it will compare the two strings (buffer.toString and str).
My Study Notes
[ March 05, 2003: Message edited by: Karin Paola Illuminate ]

I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
i seem to remember that the StringBuffer does not overide the equals method.
so if u swapped it around so that the code read
if buffer.equals(str);
result could be true.
i should check this tho.

giddee up
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Since StringBuffer does not override the equals method, there is no way that the result of
will ever be true, since a reference to a String object will always be different from a reference to a StringBuffer object.
Remember that the basic implementation of equals compares the references, that is, it performs a == comparison between the two arguments.
I agree. Here's the link:
subject: Comparing String with StringBuffer
It's not a secret anymore!