This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Hi, could anyone pls tell me why the answer to the following code (poddar's test) is false false false dar 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));
If i am right,equal method is used to test whether or not two strings are equal and == operator is ued to determine whether or not the strings are stored in the same location ie two string objects refer the same string. so,I thought the answer to the above question must be false,true,false,dar. Thanks in advance bani
Originally posted by bani kaali: If i am right,equal method is used to test whether or not two strings are equal and == operator is ued to determine whether or not the strings are stored in the same location ie two string objects refer the same string.
Yup, that's exactly right. So why was your answer wrong? We're dealing with StringBuffers, not Strings. They're quite different. StringBuffer doesn't override the equals method like String does. It simply uses the one from Object, which compares the object references. Corey
ahhhh but notice you're using .equals on a StringBuffer, and the .equals method was not overridden in StringBuffer like it was in the String class. -- Check out our Handy Dandy Search utility -- this has been discussed a bunch. [ June 24, 2002: Message edited by: Jessica Sant ]
Thanks Corey and jessica for your prompt reply. It is my mistake ,jessica u are right!! I didnt notice that equals is being used on Stringbuffer,oversight!! looks like I am becoming more and more powerful!! should buy a spectacles soon!
ah, so I was wrong. Since the API doc says it will copy the String objects' content (I assume it means the data not the reference?) into the new object, the two would have different address. Is that right? [ June 24, 2002: Message edited by: Chung Huang ]