aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer" Watch "StringBuffer" New topic
Author

StringBuffer

olddai
Greenhorn

Joined: Jan 10, 2001
Posts: 2
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

Joined: Jan 10, 2001
Posts: 2
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

Joined: Mar 17, 2000
Posts: 5782
'olddai'
Your name does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.
Ajith

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
sachin patel
Ranch Hand

Joined: Nov 28, 2000
Posts: 75
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.
****************************************************


Sachin,<P>****************************************************<BR>Learn from others mistakes. Life is too short to make all yourself.<BR>****************************************************
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer