This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

one interesting question

 
brijesh r
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not difficult but it is indeed very tricky.
 
kishor kotecha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was a good explanation Brijesh.
 
Manish Singhal
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Certainly a good way of explanation. Hope to get more explanations like that.
Manish
 
Ray Hsieh
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic