Hi
I had the similar concern few days ago but it is clarified now. I hope this example will help.
public class StringTest {
public static void main(String args[]) {
StringBuffer sb1 = new StringBuffer("ABC");
StringBuffer sb2 = new StringBuffer("ABC");
StringBuilder sbld = new StringBuilder("ABC");
String s = new String("ABC");
if(sb1.equals(sb2))
System.out.println("Equals");
else System.out.println("No they are not");
/*
Word around to compare two StringBuffer * or StringBuilder objects */
if(sb1.toString().equals(sb2.toString()))
System.out.println("Equals");
else
System.out.println("Not Equals");
if(s.equals(sb1))
System.out.println("YES");
else
System.out.println("No they are not"); if(s.equals(sbld.toString()))
System.out.println("Now they are");
else
System.out.println("We are not reaching here");
}
}
OUTPUT:
No they are not
Equals
No they are not
Now they are
NOTE: The key is StringBuffer and StringBuilder do not override equals methods, so the objects of StringBuilder and StringBuffer use equals method of object class, while this is not true of String class.
Regards
Padma