This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "code not understood" Watch "code not understood" New topic
Author

code not understood

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
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
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: 479
can you help me now

Rajat Gupta
Greenhorn

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

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
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
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.
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: http://aspose.com/file-tools
 
subject: code not understood