Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes StringBuffer Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringBuffer Question" Watch "StringBuffer Question" New topic

StringBuffer Question

Kenneth Ho

Joined: Mar 22, 2001
Posts: 18
Question 53. in Abhilash Java Quiz
I would like to know whether StringBuffer can be used to compare string. I complied and run the foll codes and it shows nothing.

--------------------<BR>Java will never die.<P>Kenneth
Johannes de Jong

Joined: Jan 27, 2001
Posts: 5089
Originally posted by Frank Carver:
The two StringBuffers don't come out as equal, because StringBuffer just uses the "default" implementation of equals(), which does the same as '==' and directly compares the object references. The two StringBuffers are different objects with different references, so they come out as non-equal.

Originally posted by Hari Gangadharan:
I really love this question! The StringBuffer *does* not override Object's "equals" method whereas String do. Hence when you invoke s1.equals(s2), the equals method of Object is invoked. The equals method of Object just does a shallow compare (which essentially is s1 == s2) and returns false!
Hope this helps --

[This message has been edited by Johannes de Jong (edited April 06, 2001).]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
The JavaRanch forum in intended for discussion of this web site, not questions about Java itself. That's what all the other forums here are for. I'm moving this discussion to Java in General (Beginner).

"I'm not back." - Bill Harding, Twister
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
Being new to OO in general, comparing objects is a concept I've been struggling with, so this is a great question. If someone wouldn't mind confirming my thoughts on this I'd appreciate it.
sb1 and sb2 are entirely seperate instances of a StringBuffer object complete with their own references and that is why they aren't equal, because the references are different?
If so, how could they ever be equal? I assume only by assigning ones value to the other like "sb1 = sb2". What happens when this is done, is the sb2 OBJECT actually deleted (or ready for GC?) so that you now have two references to the sb1 OBJECT?
If so, what happens if I change sb1, is sb2 also changed? If this is true, what is the value in having multiple references to the same OBJECT?
Back to the original question, how should this comparison be handled, something like this? (code is psuedo-code: the method names might be wrong)

Sorry if this is too many questions at once but I thought they were all related. I feel on the verge of understanding this, which would be a big boost...
Joel Cochran
[This message has been edited by Joel Cochran (edited April 06, 2001).]

Wait a minute, I'm trying to think of something clever to say...<p>Joel
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Try this:
String s = sb1.toString();
String t = sb2.toString()
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Joel, your thinking is correct.
The only way for two object references to be equal is if they both refer to the same object.

You are right that when you set one reference equal to another, then if there are no *other* references to the object it is eligible for garbage collection. But check your logic on this:
if you say "sb1=sb2", then which object is eligible for GC? It's "the object formerly refered to by sb1". not sb2, like you mentioned.
[This message has been edited by Mike Curwen (edited April 06, 2001).]
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
I think I get it... sb1 takes on the values of sb2 (of course, I just hadn't thought it through...)
But again I have to ask...what's the point? Why not just set sb2 to null and drive on with sb1?

Joel Cochran
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: StringBuffer Question
It's not a secret anymore!