aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "one interesting question" Watch "one interesting question" New topic
Author

one interesting question

brijesh r
Greenhorn

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");
method(s1,s2);
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

DS_Malik
Greenhorn

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
Greenhorn

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
yes.
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
b1=b2
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
s1=s2
then u will get both same content
(if anybody knows better pls let me )
Prasad
------------------
brijesh r
Greenhorn

Joined: Oct 03, 2000
Posts: 4
representing it graphically makes things clearer,
let me try explaining it like this.
Initial state
s1---->[seventh]
s2----[seventh]
for the method a copy of the references are sent
so we have
s1---->[seventh]<-----b1
s2----[seventh]<-----b2
string buffer is mutable so the append adds to the same object,
so after the method call this is the status
s1---->[seventh]
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
regards
brijesh
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.
Manish
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: http://aspose.com/file-tools
 
subject: one interesting question
 
Similar Threads
about String & StringBuffer
StringBuffer var as an argument
String concatenation
StringBuffer??
stringBuffer hashcode