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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "one interesting question" Watch "one interesting question" New topic

one interesting question

brijesh r

Joined: Oct 03, 2000
Posts: 4
i found this in one of the mocks and got a revised version in my JCP, i will frame it my way:
StringBuffer s1 = new StringBuffer("seventh");
StringBuffer s2 = new StringBuffer("seventh");
System.out.println("s1 is" + s1 + " s2 is " + s2);
void method(StringBuffer b1, StringBuffer b2)
b2.append(" cloud");
b1 = b2;
Guess what is the output??
the answer is s1 is seventh
and s2 is seventh cloud


Joined: Oct 02, 2000
Posts: 5
Good Mock Question,Even I had wrong answer first time,Then looked very closely and compiled it for the right answer.
Thanks for posting it.

Harry Singh
Ranch Hand

Joined: Sep 22, 2000
Posts: 53
It is not difficult but it is indeed very tricky.
kishor kotecha

Joined: Oct 02, 2000
Posts: 27
Sorry I could not understand why does it behave like that. s1 and s2 are both object of the string class. Then they are passed to method() (by referenec) which receives them as StringBuffer class. Then b2 changes itself by appending. if b2 changes s2, b1 becomes b2 so why s1 does not change? any answers?
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
The answer is correct the reason is b1 and b2 are reference variable pointing to s1 object and s2 object respe.Once u change the content of s2 object through b2 reference variable it will directly change the s2 object content,and after that once come to this code
here u are changing the object reference not the s1 object .hence the s1 object content remains the same.
BUT if u try to do this
then u will get both same content
(if anybody knows better pls let me )
brijesh r

Joined: Oct 03, 2000
Posts: 4
representing it graphically makes things clearer,
let me try explaining it like this.
Initial state
for the method a copy of the references are sent
so we have
string buffer is mutable so the append adds to the same object,
so after the method call this is the status
s2----[seventh cloud]<-----b2, b1
and now what we are printing are s1 and s2 which is ofcourse seventh and seventh cloud
hope this is clear
Viji Bharat
Ranch Hand

Joined: Sep 18, 2000
Posts: 101
That was a good explanation Brijesh.
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Certainly a good way of explanation. Hope to get more explanations like that.
Ray Hsieh
Ranch Hand

Joined: Sep 20, 2000
Posts: 59
For you with C/C++ background, maybe it helps to note that everything in Java is passed by value. That is, even the reference type is passed by value. You can NOT change to where the reference is "pointing", you can only change the poor thing being pointed by it
I agree. Here's the link:
subject: one interesting question
It's not a secret anymore!