• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in StirngBuffer

 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one throw some light on why this expression " (sb1.equals(sb2) " evaluates to false in the following piece of code.



Output :
sb1 == sb2 false
sb1.equals(sb2) false
sb3 == sb1 true
sb3.equals(sb1) true
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In looking at the documentation, StringBuffer doesn't override the equals method from Object which simply determines if two object references point to the same object.
 
Divya Mehrotra
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer class DOES NOT override the equals() method. Therefore, it uses Object class' equals(), which only checks for equality of the object references. StringBuffer.equals() does not return true even if the two StringBuffer objects have the same contents:
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies. i got it.
 
siva prakash
Ranch Hand
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am getting compiler error when i tried following, can any explain why its happening



thanks in advance

Prakash S
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is confusion because of the precedence of operators.

Change the line to this.

System.out.println("output is : "+ (st1==st2));
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by siva prakash:
Hi all,

I am getting compiler error when i tried following, can any explain why its happening...


For future reference, you should start your own thread when asking a new question. Also, it is VERY helpful if you post the exact error message that you get. Otherwise, we are only guessing what the problem is. I don't know about others here at the Ranch, but Most of the time, I won't take the time to compile the code you post. (That's assuming that you provide enough code to compile.) So please post your error messages.

Thank you,

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic