Forums Register Login

code not understood

+Pie Number of slices to send: Send
I compiled and got output

But not understood why I got out as--- 15 0 20

Do help me step by step clearly please.

class Value
{
public int i = 15;
} //Value
public class Test
{
public static void main(String argv[])
{
Test t = new Test();
t.first();
}
public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i)
{
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
} // Test
+Pie Number of slices to send: Send
Please post your code in between this:


and then post your question again. It is very difficult to read your post otherwise. Here is how it looks:

public void method() {
S.o.pln ("hello world");
}


+Pie Number of slices to send: Send
can you help me now

+Pie Number of slices to send: Send
Hey kartik,

This is what I understand.

Objects in Java are always passed through the method calls "BY VALUE" and never "BY REFERENCE".

Now, lets go through the code.

After the t object is created, it calls the method first(). Here object v gets defined, with values as -- v.i =25
Then a call to the second() method is made. Here, a copy of the object reference is sent to the second method. That is, if v in first is pointing to some object, the copy of that object address is passed to second method. Lets take that address to be obj123.
So, the value of v passed -- obj123

IN SECOND METHOD

Value received in second method v = obj123.
v.i changed from 25 to 20

In second method, another new Value object val is created. Lets take that val conatins -- obj456
Now, val.i = 20

After v = val

v (in second method ) = obj456

Therefore, in second method print statement, 15 and 0 are printed.


IN FIRST MEHOD
Now, when it goes back to the first method, the value of v is = obj123, beacuse, you have just passed the copy of the address of the object that v was pointing out to and not the object itself. Even if that value of the copy was changed in the called method (second method) , it wont affect the original copy of the object address in the calling method.
So, the value of v.i = 20 is printed.

I hope this clarifies the doubt.

Cheers,
Rajat upta
+Pie Number of slices to send: Send
 


Objects in Java are always passed through the method calls "BY VALUE" and never "BY REFERENCE".




Objects in Java are always passed through the method calls "BY Reference" and never "BY Value".
+Pie Number of slices to send: Send
i think if you do not repeat the variable names it will be lot clearer.

like you said the output would be
15 0
20

Value v = new Value();v.i = 25; // create a new Value object with referene v and memeber 25.

second(v, i);// calling second method with v and i from above as parameters.

in the second() method you take these parameters as v and i. now these are new references visible only in the second() method. however with this new v reference you could still modify the referenced object member variable like i (which is what you do by saying v.i = 20).
then you make a new Value object and have the v reference ( in the seond() method) to point to this new object. which is why it has memeber value 15.
so it prints 15 and 0.

now control comes back to first() method. and it proints 20 coz u have changed the object.

rememebr when going between methods the object remains the same, but the refereences change (pass by 'copy').

hth.
+Pie Number of slices to send: Send
Hi Naresh,
Java always passes parameters by value and not by reference. If they are primities then a copy of the value is passed. If it is an object(reference), a copy of the reference is passed. Because the copy also refers to the original object, the object referred by the copy(method parameter) changes.
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1190 times.
Similar Threads
please explain
strange behaviour of reference variable when passing to a function
What will be the output??? Help me please
java about pass by value,why?
Reference passing
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:40:13.