Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer

 
Arsho, Ayan
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<code>
will this code always return false
StringBuffer s1 = new StringBuffer("Java");
StringBuffer s2 = new StringBuffer("Java");
System.out.println(s1.equals(s2));
Would appreciate your reply
Thanks
</code>
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 08, 2001).]
 
Thomas Bigbee
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic