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 code not understood 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 "code not understood" Watch "code not understood" New topic

code not understood

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
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();
public void first()
int i = 5;
Value v = new Value();
v.i = 25;
second(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
Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81
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");

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
can you help me now

Rajat Gupta

Joined: Sep 16, 2005
Posts: 7
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


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.

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.

Rajat upta
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163

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".
praveen Shangunathan
Ranch Hand

Joined: Sep 06, 2005
Posts: 65
i think if you do not repeat the variable names it will be lot clearer.

like you said the output would be
15 0

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').

Cheenu Subramanian
Ranch Hand

Joined: Aug 15, 2005
Posts: 40
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.
I agree. Here's the link:
subject: code not understood
It's not a secret anymore!