This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer Question

 
Kenneth Ho
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

And
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
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Try this:
String s = sb1.toString();
String t = sb2.toString()
if(s.equals(t))
Thanks,
Angela
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic