Here is a question and the answer . Q 1. What is the output of the following StringBuffer sb1 = new StringBuffer("Amit"); StringBuffer sb2= new StringBuffer("Amit"); String ss1 = "Amit"; System.out.println(sb1==sb2); System.out.println(sb1.equals(sb2)); System.out.println(sb1.equals(ss1)); System.out.println("Poddar".substring(3)); Ans: a) false false false dar
I understood why everything else is false except the second false. Here 2 stringbuffers are being compared using equals method and they contain the same string "Amit". Why is it giving false ? Thanks, Srini
I might be wrong on this, but the String class .equals() compares the contents of 2 strings. I' am not sure if this holds true for the StringBuffer class. Is essence it is really comparing 2 Objects not 2 String Objects, so this example is using the Object .equals() and not the String .equals(). Please correct me if I am wrong.
Hi: Mark is correct..When you are using the equals() on 2 StringBuffers which are same , you still get a false value is because of the following (I guess...): - The StringBuffer class does not override the equals() provided by the Object class. Hence you are actually calling the Object's version of equals(); and this version does a "shallow" comparision i.e. it does a "==" comparision and hence it returns a false. Hope the above helps. Fazal
posted 19 years ago
Thanks to all of you. Srinivas
The moth suit and wings road is much more exciting than taxes. Or this tiny ad: