*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on References 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 "Question on References" Watch "Question on References" New topic
Author

Question on References

Pallavi Chakraborty
Ranch Hand

Joined: Jan 18, 2003
Posts: 93
Hey everybody,
The answer to the following question is 10020.
I am not getting the 20 part of it.

Thank you
Pallavi
Monisha Talwar
Ranch Hand

Joined: Feb 11, 2003
Posts: 102
Hi Pallavi,
I think you have an extra closing bracket up top, which puts your methods outside the class...I think thats a typo. I also think the your out put would be 3010020.
Ok, here are the steps:
1. Obparm o = new Obparm(); creates and instance of Obparm.
2.o.amethod(); Lets get into the amethod()
amethod()
3. int i = 99;
4. ValHold v = new ValHold();makes a new instance of v. So now v.i is back to 10.
5. v.i=30; Ok, are now over writing v.i with 30.
6. System.out.print(v.i); - This should print the first number 30
7. another(v,i); Call another() method with v which refers to the 30 guy and i which is still 99.
another()
8. i=0; Local copy of i is not 0.
9. v.i = 20; Here we are seeing the actual data in v to 20. Even though v is only a copy of the reference, it still points to the same object. So it sets the i to 20 in the original instance.
10. ValHold vh = new ValHold(); What we did here is made a new instance of ValHold. So vh.i = 10.
11. v = vh; Ok here we are changing the copy of the referance we got, to point to the new instance we created. Only the copy is being changed. The original referance is still there.
So now v.i = vh.i = 10
12. System.out.print(v.i); Here you get the second number 10
13. System.out.print(i); Here you get the third number 0 coz the local copy of is 0.
14. Now back to amethod()
15. System.out.print("the last one" + v.i ); Here you get your last number 20. This is because you original copy of v in another method still refers to the original ValHold instance. But remember we changed the data in this instance to 20. See step 9.
Hope this helps.
Monisha


Hey! It compiles! Ship it!
Pallavi Chakraborty
Ranch Hand

Joined: Jan 18, 2003
Posts: 93
Hey Manisha,
So, here we are passing a copy of the refernce v into the method another.
I was not sure of what:
v.i = vh.i = 10 is doing.
Because we changed v.i to 20 and then v.i to 10 again.
So, can you explain why we are again making a copy for the above code when we already got a copy of v into the method another (v.i = 20).
Thank you
Pallavi
Aarti Joshi
Greenhorn

Joined: Mar 31, 2003
Posts: 24
Hi,
here is another example which can clear "pass by value" and "pass -by-reference"
class Demo
{
public int value =111;
public void setValue(int v)
{
value = v;
}
}
public class PassByValue{
public void usePrimitiveData(int d)
{
d = d+1;
}
public void changeReferenceValue(Demo d)
{
d = new Demo();
d.setValue(555);
}
public void useReferenceValue(Demo d)
{
d.setValue(999);
}
public static void main(String[] args)
{
PassByValue pass = new PassByValue();
Demo demo = new Demo();
int value =444;
System.out.println("Demo's value is " + demo.value);
System.out.println("Value is " + value);
pass.usePrimitiveData(value);
System.out.println("Value is " + value); // does not change original value
pass.changeReferenceValue(demo);
System.out.println("Demo's value is " + demo.value); // does not chane the original copy of data
pass.useReferenceValue(demo);
System.out.println("Demo's value is " + demo.value); // changes the original copy of data
}
}

-- so whenever we pass an argument to a method, we always make a copy of the argument.
What happens depends upon the type of argument : Primitive data or Reference.
When we use referense : we either change the ORIGINAL value through reference or we ALTER the value of reference.
-- cheers
Aarti
Sarbani Mookherji
Greenhorn

Joined: Feb 25, 2003
Posts: 14
Hello Pallavi,
----------------------------------------------
So, here we are passing a copy of the refernce v into the method another.
I was not sure of what: v.i = vh.i = 10 is doing. Because we changed v.i to 20 and then v.i to 10 again.So, can you explain why we are again making a copy for the above code when we already got a copy of v into the method another(v.i = 20).

-----------------------------------------------
In the line v = vh we only make the reference v point to the reference vh.Here v is just a copy of the original reference.The new reference vh has the original value of i which is 10, which again will be the same(i.e,10) for every new instance of the class ValHold created.
When v->vh then i in v get the value 10 as it is 10 in vh.
I think changing v.1 to 20 and then to 10 again is just a trick to test our knwoledge on the topic and nothing more.
I hope I am right the way I am thinking and could help you.
Best Wishes,
Sarbani.


They say..if you try sincerely you succeed...lets see!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on References