my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes stringBuffer VS string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "stringBuffer VS string" Watch "stringBuffer VS string" New topic
Author

stringBuffer VS string

Ray Chang
Greenhorn

Joined: Sep 10, 2002
Posts: 25
when declare:
StringBuffer sb1=new StringBuffer("abcd");
StringBuffer sb2=new StringBuffer("abcd");
String s1=new String("abcd");
String s2=new String("abcd");
Is sb1==sb2?
Is s1==s2?
Why?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
The answer is NO and NO. With each new your are creating a new object. The == compares for whether operands point to same/different objects. In this case objects are identical but different so answer is NO.
Ray Chang
Greenhorn

Joined: Sep 10, 2002
Posts: 25
i'm sorry I typed wrong thing on.
My question is
Is sb1.equals(sb2); ?
Is s1.equals(s2); ?
Thank you very much
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
up down,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Is sb1.equals(sb2); ?
No. Because StringBuffer inherites the equals method from Object class which is same as == comparision. As these two different objects, result be false
Is s1.equals(s2); ?
Yes. Because String overrides the equals method from Object class. String' equals method checks if two string objects have same contents.
Hope this helps.
Barkat
[ September 12, 2002: Message edited by: Barkat Mardhani ]
Ray Chang
Greenhorn

Joined: Sep 10, 2002
Posts: 25
display name changed.
 
 
subject: stringBuffer VS string
 
Similar Threads
Difference of equals method in string and stringbuffer
StringBuffer
StringBuffer Question???????
String & String Buffer
arguments to method