StringBuffer equals method

P

priyom

Hi,

I found the following behaviour in StringBuffer and StringBuilder
equals method.

StringBuffer sb1 = new StringBuffer("ABC");
StringBuffer sb2 = new StringBuffer("ABC");//It can be any string same
as above
System.out.println(sb1.equals(sb2)); //this prints false!


Could someone please explain this behaviour.

Thanks in advance,
Priyom
 
A

Andrew Thompson

priyom wrote:

<sscce>
class TestEquals {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("ABC");
//It can be any string same as above
StringBuffer sb2 = new StringBuffer("ABC");

// compare the StringBuffer's for equality
//this prints false
System.out.println(sb1.equals(sb2));
// compare the StringBuffer's *contents*
// for equality ;-)
//this prints true
System.out.println(sb1.toString().
equals(sb2.toString()));
}
}
Could someone please explain this behaviour.

What is your explanation, based on the above code?

Andrew T.
 
R

redbox

StringBuffer 's equals method inherited from class java.lang.Object
only if sb1 and sb2 refer to the same object return true; the same to
operator "==";
class String equals methode is overridded ;
 
A

Andrew Thompson

redbox said:
only if sb1 and sb2 refer to the same object return true; the same to
operator "==";
class String equals methode is overridded ;

Note that the addition of a '>' character to the beginning of
your first statement makes it appear (at a glance) that it
was made by someone else, someone that you are quoting.

Andrew T.
 
P

priyom

Andrew said:
priyom wrote:

<sscce>
class TestEquals {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("ABC");
//It can be any string same as above
StringBuffer sb2 = new StringBuffer("ABC");

// compare the StringBuffer's for equality
//this prints false
System.out.println(sb1.equals(sb2));
// compare the StringBuffer's *contents*
// for equality ;-)
//this prints true
System.out.println(sb1.toString().
equals(sb2.toString()));
}
}
Could someone please explain this behaviour.

What is your explanation, based on the above code?

Andrew T.

Well my question was actually regarding the implementation of "equals"
method of StringBuffer and not String. I was under the impression that
in equals implementation, the contents of the StringBuffer i.e. the
String value inside the StringBuffer determines the equality (but
apparently it is not so).

Regarding the above example, two Strings check their contents if they
are meaningfully equivalent and if so return true, as above.
That is the reason I was confused regarding the StringBuffer behaviour
which seemed to be different from that of String or some other standard
implemntations like Integer, Float, etc.

I just checked and found that StringBuffer class in fact does not
override Object.equals. Hence two StringBuffer objects would be equal
only if they refer to the same object.

Thanks for your help.
 
L

Lew

priyom said:
I just checked and found that StringBuffer class in fact does not
override Object.equals. Hence two StringBuffer objects would be equal
only if they refer to the same object.

Which is exactly what the other respondents told you.

- Lew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top