public class Test{ public static void main(String args[]) { StringBuffer o = new StringBuffer("abcd"); StringBuffer s = new StringBuffer("abcd"); System.out.println(o.equals(s)); } } the running result is false,but I thought it should be true, is there any difference between 'equals'method of StringBuffer and that of String?
because there're two objects(two new). try StringBuffer s=o and you will get true from api: public boolean equals(Object obj)Indicates whether some other object is "equal to" this one. ...... The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
The StringBuffer class does not override the equals() method. So it calls the equals() method of the Object class which tests for reference equality. Since the String class and all the primitive wrapper classes override the equals() method, you would expect StringBuffer to also, but not so.