• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

stringBuffer VS string

 
Ray Chang
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
display name changed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic