class test1 { static public void main(String args[]) { String sb1 = new String("Amit"); String sb2= new String("Amit"); String ss1 = "Amit"; System.out.println(sb1==sb2);//false, System.out.println(sb1.equals(sb2));//true System.out.println(sb1.equals(ss1));//true } }
class test1 { static public void main(String args[]) { StringBuffer sb1 = new StringBuffer("Amit"); StringBuffer sb2= new StringBuffer("Amit"); String ss1 = "Amit"; System.out.println(sb1==sb2);//false System.out.println(sb1.equals(sb2));//false System.out.println(sb1.equals(ss1));//false } } Is the output as expected in case of string buffer??
If you check the API, you'll see that StringBuffer does not override the equals method, so it invokes the method as defined in Object (which is basically ==).
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org