• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion on equlas and == for string buffer and string builder  RSS feed

 
Swapna latha
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have written a sample program on equals and == for string buffer and string builder. As per my knowledge (which is right or wrong) equals will compare the two objects bit by bit and == compares two memory locations.

But its not working in case of string buffer and string builder. I am getting strange error when using == with string buffer and string builder and it is "incompatible operand types String and StringBuffer"



I am getting confusion here.

thanks

swapna
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The == operator (when used on reference types) checks if the two operands refer to the exact same object.

When you try to compare to unrelated kinds of objects, for example a StringBuffer and a StringBuilder (they are not related through inheritance), the result will always be false - a StringBuffer object can never be the same object as a StringBuilder object. Since this is a useless comparison, the Java compiler doesn't allow you to compare them - it gives you an error message that tells you you can't compare two unrelated kinds of objects with ==.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swapna latha wrote:As per my knowledge (which is right or wrong) equals will compare the two objects bit by bit and == compares two memory locations.

Sort of, but you're being far too literal. equals() compares two objects for logical equality, '==' compares them for identity (ie, it will only return true if the two objects are the same object).

The fact is that you should almost never use '==' with objects, so spending a lot of time testing how it works, while possibly interesting, may not be the best use of your time.

For more information, read the AvoidTheEqualityOperator (←click) page.

Winston
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that even when you have fixed the compilation problems you are not going to get the results you expect.
StringBuffer and StringBuilder do not override the equals method, so they will inherit it from the Object class and the Object class just compares the two objects using ==.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swapna latha wrote:But its not working in case of string buffer and string builder. I am getting strange error when using == with string buffer and string builder and it is "incompatible operand types String and StringBuffer"


It is an order-of-operations thing. When being resolved, this will probably become System.out.println("In buffer hellow world"==s3); and you have a String being compared to StringBuffer. Put parenthesis around the s3==s3.
 
Swapna latha
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all. I got the concept. This forum is really awesome.

Thanks once again

swapna
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!