This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer" Watch "StringBuffer" New topic


Arsho, Ayan
Ranch Hand

Joined: Nov 14, 2001
Posts: 60
will this code always return false
StringBuffer s1 = new StringBuffer("Java");
StringBuffer s2 = new StringBuffer("Java");
Would appreciate your reply
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
StringBuffer does not override the equals method and thus inherit the equals method of class Object which basically performs a ==. Thus it will return false since you have two
different references to StringBuffer.
Moreover, if you try to compile and run your code you get the answer the same second !
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 08, 2001).]

[Blog] [Blogroll] [My Reviews] My Linked In
Thomas Bigbee
Ranch Hand

Joined: Nov 29, 2001
Posts: 48
You are correct - the equals method is inherited from the Object class which tests if two object are equal - ie - points to the same object reference - and since StringBuffer is a final class, and inherits the Object.equals method() - you can not override any of its methods.
At least I think I am correct.
Hope that helps, Tom
I agree. Here's the link:
subject: StringBuffer
Similar Threads
No NullPointerException?
Inner Thread Classes
== and equals
String and String buffer
please help