Hello evryone, i've been going thru poddar's q's on strings and the very first one caught me(lacking precise knowledge about strings and stringbuffers..) 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 b) false true false Poddar c) Compiler Error d) true true false dar Correct Answer is a) for this ," System.out.println(sb1.equals(sb2)); " ...my thinking is that... since equals compares deeply(or the strings and not references),the o/p should be true.
same with the " System.out.println(sb1.equals(ss1)); " the o/p should be true. I couldn't reason my thinking with the given answer..
If anybody is wondering how Dan could possibly know that StringBuffer doesn't override the equals method, you might like to familiarize yourself with the J2SE API documentation. The StringBuffer class documentation (as well as the documentation for other classes) lists what methods the class defines, and what methods it inherits.