aspose file tools*
The moose likes Beginning Java and the fly likes Doubt in StirngBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in StirngBuffer" Watch "Doubt in StirngBuffer" New topic
Author

Doubt in StirngBuffer

Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
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


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Mar 01, 2005
Posts: 21
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:


Divya Mehrotra
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Thanks for your replies. i got it.
siva prakash
Ranch Hand

Joined: Aug 24, 2004
Posts: 80
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in StirngBuffer