The method equals of String override that method of in Object ,so it is design to compare to see whether the value of the two string have the same value.But in the StringBuffer there's not the equals method ,that means if use the method in Object,which only compare to see if the two object are the same. I try my conclusion above ,but according the StringBuffer,it is not the true,so I am confused, could sb tell me that ,in what condition the equals is only to compare the value of the objec ,but not to compare for a result whether the two object are the same object.
Hi, the equals() method is overridden in all the wrapper classes and the String class in java.lang package. equals() method in StringBuffer class behaves normally (as it does with anyother class) see the following declaration, StringBuffer sb1= new StringBuffer(); StingBuffer sb2= new StringBuffer(); using new operator allocates memory space for the variable on the heap,and hence these two variables (sb1, sb2) have two different references on the heap.
now sb1.equals(sb2) returns false. and if the statement sb1=sb2; preceeds the check sb1.equals(sb2) then it returns true. Hope this has helped your understanding, otherwise mail back.
The equals() method is defined for class StringBuffer, and checks for equivalence of StringBuffer objects. Bear in mind that the following code: StringBuffer sb = new StringBuffer("test"); System.out.println(sb.equals("test")); ...will print false because the literal "test" is a String object, not a StringBuffer, and therefore not equal, whether it contains the same characters or not. The following code: StringBuffer sb1 = new StringBuffer("test"); StringBuffer sb2 = new StringBuffer("test"); System.out.println(sb1.equals(sb2)); ...will print true.
Joined: Oct 15, 2001
To James Thomson: StringBuffer sb1 = new StringBuffer("test"); StringBuffer sb2 = new StringBuffer("test"); System.out.println(sb1.equals(sb2)); will print the false not true.
To abdul khayyum: I think you are right .Thank you.
Joined: Nov 19, 2001
Blimey! You're right! How strange that equals() hasn't been overridden in StringBuffer! Well that's worth knowing, thanks! Guess I was jumping to conclusions.
Ok, this has always baffled me, so once and for all. For objects what does == do and what does .equals() do? And could I get an example? Like Object obj1 = new Object(); and Object obj2 = new Object(); What does obj1==obj2 yeild and alternatively what does obj1.equals(obj2) yield? With strings and with wrapper clases, am I correct in saying that the == operator compares references for equality and .equals() compares values for equality? For instance Integer int1 = new Integer(1); and Integer int2 = new Integer(1); then int1==int2 will yield false and int1.equals(int2) will yeild true, correct? This is the same with strings, but StringBuffers are not overriden and so they perform the same way as the object comparison operators, correct? Also, it is obvious that with a String or a wrapper class you can compare a value or a reference, but with objects, it does not seem that there is any value to compare, so does == and .equals() in object just compare reference (meaning they do the exact same thing)? And in StringBuffer, even though there is a value to compare, does it only check reference when using .equals() or ==? Sorry for the long runon question. I am very appreciative to anyone who can take the time to answer it. Thanks, Matt Wheeler
Matt, == compares object references for reference equality and primitive value for value equality, i.e. Object o1 = new Object(); Object o2 = new Object(); Object o3 = o1; o1==o2 yields false; o1==o3 yields true; int i1 = 1; int i2 = 1; int i3 = 2; i1==i2 yields true i1==i3 yields false Now concerning equals, first of all, equals can only be applied to object references since it is a method. You should always override the equals method in your classes in order to provide a correct equality test for your objects since the equals method in Object just returns the result of an == test. Moreover, only objects of the same class should be compared equal, that is you cannot compare a Long and a Integer for equality. Long l = new Long(1); Integer i = new Integer(1); l.equals(i) or i.equals(l) yields false HIH ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform [This message has been edited by Valentin Crettaz (edited November 20, 2001).] [This message has been edited by Valentin Crettaz (edited November 20, 2001).]