File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer question?" Watch "StringBuffer question?" New topic
Author

StringBuffer question?

Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
Here is the question.
Q 1. What is the output of the following ?


Ans:
a) false
false
false
dar

b) false
true
false
Poddar

c) Compiler Error

d) true
true
false
dar

The correct answer is a.
What I noticed was that the StringBuffer class seems to behave different when it comes to the equals method. For example, if I changed all the instances of StringBuffer in the above example to Strings. The output was false, true, true, dar. To be honest I thought that would have been the answer to the actual question, obviously I have gone wrong somewhere.
Could somebody please help me out here, please?
Thanks, Dave.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
The StringBuffer's equals() method is not overridden, meaning that it is using the Object's equals() method. And as we all know, Object's equals() performs similarly as the '==' operator. So the statement
sb1.equals(sb2)
is the same as
sb1 == sb2
[ September 13, 2003: Message edited by: Alton Hernandez ]
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
Thankyou Alton Hernandez, that makes perfect sense, I appreciate your help, Dave.
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
Sorry to be a pain, but what about this code:

Now thanks to Alton I know that the equals() method applied to the stringbuffers in this code act only as == operators. So as far as I can see they both refer to the same object.
What I originally found confusing was the line: StringBuffer sb1=sb.reverse();.I found it strange that applying the reverse() method to sb1 changes sb aswell. Just to humour me please just answer if this explains it. By assigning this new string to sb1 you also assign it to sb, as they are the same object? Am I anywhere near right?
Thanks, Dave.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Dave Johnson:

What I originally found confusing was the line: StringBuffer sb1=sb.reverse();.I found it strange that applying the reverse() method to sb1 changes sb aswell. Just to humour me please just answer if this explains it. By assigning this new string to sb1 you also assign it to sb, as they are the same object? Am I anywhere near right?

Hi Dave,
Not exactly the right picture. I think it is more of the other way.
The reverse() method modifies the value of StringBuffer(StringBuffers are mutable). It does not create a new StringBuffer. So what that method is returning is itself. Therefore I think it is more right to say that the StringBuffer sb with its new value is also assigned to sb1.
Hope this helps.
[ September 14, 2003: Message edited by: Alton Hernandez ]
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
Thanks Alton!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer question?