Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== & equals( ) in StringBuffer

 
sadas dsdasd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
StringBuffer sb1=new StringBuffer("abc");
StringBuffer sb2=new StringBuffer("abc");
if(sb1.equals(sb2))
I think it returns false as StringBuffer doesn't override equals() method.If I am wrong please correct me.
What is the result if it is like this.
StringBuffer sb1=new StringBuffer("abc");
StringBuffer sb2=sb1;
what does the following comparisons return?
1)if(sb1==sb2)
2)if(sb1.equals(sb2))
please help me.Thanks in advance.
 
Praveen Zala
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to the first Question is correct !!
The answer to 2a) is it returns true as there is only one
reference at sb1 !!
The answer to 2b) is also true as it is the same stringbuffer
object
Praveen
 
sadas dsdasd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
Q.StringBuffer sb1=new StringBuffer("abc");
StringBuffer sb2=new StringBuffer("abc");
What does this return?
if(sb1==sb2)
Q.String s1=new String("abc");
String s2=s1;
what does the following comparisons return?
A.if(s1==s2)
B.if(s1.equals(s2))
please give me the answer.Thanks in advance.
 
sadas dsdasd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to the first one is false because sb1 and sb2 are two different references.
s1 == s2 is true since both refer to the same reference.
s1.equals(s2) is true because if the two string references are same then this has to be true.
 
sadas dsdasd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot carl.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic