• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer

 
olddai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, every one
Why the following code got the output "false"?
Is the Stringbuffer the same as String when use the equals() method?
StringBuffer sb1 = new StringBuffer("Amit");
StringBuffer sb2 = new StringBuffer("Amit");
System.out.println(sb1.equals(sb2));
Thanks!
 
olddai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find the answer by myself. The reason is:
StringBuffer's equals just checks for equality of the object references. Note: Object's equals() is not overridden.
Originally posted by olddai:
Hello, every one
Why the following code got the output "false"?
Is the Stringbuffer the same as String when use the equals() method?
StringBuffer sb1 = new StringBuffer("Amit");
StringBuffer sb2 = new StringBuffer("Amit");
System.out.println(sb1.equals(sb2));
Thanks!

 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'olddai'
Your name does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.
Ajith
 
sachin patel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can chech if two StringBuffer have same value by doing
sb1.toString().equals(sb2.toString())
This is what i think... correct me if i am wrong..
StringBuffer class doesnot override the equals(Object o) method..
------------------
Sachin,
****************************************************
Learn from others mistakes. Life is too short to make all yourself.
****************************************************
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that would be one way to do it, eventhough not the most efficient way. Remember toString() creates a string object in memory and so, everytime you want to compare the StringBuffer objects, you are creating two string objects just for the sake of comparison. You may not be able to use these objects at a later time in the program since, if the content of the StringBuffer changes, the String object in memory will not be updated!!.
StringBuffer is a poorly designed Java class. Lack of content comparison makes it a handicapped class and because it is a final class, you cannot even subclass it and provide better methods.
In one of my projects I wrote a static method that would compare two StringBuffer objects without causing the memory overhead. I first compared the lengths of two objects and then compared the characters contained in them one by one. StringBuffer has a method( getChars ) that can return you an array of characters and you can use a for-loop to compare characters at the same index. This way you are not creating extra "orphan" String objects in memory and you don't have to worry about keeping the object sync'ed. I extended the functionality by supporting case-sensitive comparison and it works perfectly fine.
This concept, though not important for Certification, is worth understanding.
Ajith
[This message has been edited by Ajith Kallambella (edited January 10, 2001).]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic