Hi,All, I have problem for the following question,could you please kindly help out? Thanks a lot. my question is why the result of "System.out.println(sb1.equals(sb2)); " is false? 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)
Guoqiao Sun
Ranch Hand
Joined: Jul 18, 2001
Posts: 317
posted
0
Hi, Wang, the reason is that StringBuffer does not override equals method from Object. As a result, equals method applied to two StringBuffer objects has the same effect as that of == applied to two StringBuffer objects. Hope it helps. ------------------ Guoqiao Sun Sun Certified Programmer for Java 2 Platform try my mock exam¹² at my homepage.
Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Gurpreet Sachdeva
Ranch Hand
Joined: Feb 20, 2001
Posts: 90
posted
0
Hello Kathy, It is because StringBuffer use the equals method of the object class.Object class's equals method compares the reference of the two objects.So, the result is false. Regards Gurpreet Sachdeva For Mock Exams, FAQ and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: http://www.go4java.20m.com
Regards<BR>Gurpreet Sachdeva<P>For Mock Exams, FAQ, Exam tips and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: <A HREF="http://www.go4java.lookscool.com" TARGET=_blank rel="nofollow">http://www.go4java.lookscool.com</A>
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.