aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes had a question about string equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "had a question about string equals method" Watch "had a question about string equals method" New topic
Author

had a question about string equals method

bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

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 ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
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

Joined: Jun 21, 2002
Posts: 56
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;


Let us be showered in the light of confusion!
bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
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

Joined: Mar 22, 2002
Posts: 1055

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

Yes there is. See the docs
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
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

Joined: May 05, 2002
Posts: 42

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

Joined: May 05, 2002
Posts: 42
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: had a question about string equals method