Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

had a question about string equals method

 
bani kaali
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
bani kaali
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Chung Huang
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have the following:
StringBuffer objOne = new StringBuffer ("me");
StringBuffer objTwo = new StringBuffer (objOne);
then would the following statemet evaluate to true? Assume StringBuffer class has constructor that takes object:
objOne == objTwo;
 
bani kaali
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chung,

StringBuffer objOne = new StringBuffer("me");
StringBuffer objTwo = new StringBuffer (objOne);

There is no constuctor in StringBuffer class which takes String object as argument.
-bani
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There is no constuctor in StringBuffer class which takes String object as argument.

Yes there is. See the docs
 
Chung Huang
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
bani kaali
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

StringBuffer objOne = new StringBuffer ("me");
StringBuffer objTwo = new StringBuffer (objOne);
then would the following statemet evaluate to true?
objOne == objTwo;

no , it will not evaluate to false

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?

yes, check this out.

public class Strcheck {
public static void main(String args[]) {
StringBuffer st1 = new StringBuffer("me");
String str = st1.toString();
StringBuffer st2 = new StringBuffer(str);
if (st1 == st2)
System.out.println("stringbuffers are equal");
else
System.out.println("Stringbuffers are not equal");
if(st1.equals(st2))
System.out.println("equals mtd works");
else
System.out.println("equals mtd fails");
}
}
 
bani kaali
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, i meant

StringBuffer objOne = new StringBuffer ("me");
StringBuffer objTwo = new StringBuffer (objOne);
then would the following statemet evaluate to true? Assume StringBuffer class has constructor that takes object:
objOne == objTwo;

this will evalute to false.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic