aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Green mock 2, #51 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 "Green mock 2, #51" Watch "Green mock 2, #51" New topic
Author

Green mock 2, #51

Indy
Ranch Hand

Joined: Apr 24, 2000
Posts: 44
I remember I saw someone post asking about Green mock 2, #51 on the past Sunday. And the other guy gave a specific explaination after that. However I can not find it anymore in this column. Anybody can tell me where the post is moved to? or who is find enough to post it again. I will paste the question once again as following. Thanks a lot.
Indy
Question 51)
Given the following code what will be the output?
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.println(v.i);
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
}
1) 10,0, 30
2) 20,0,30
3) 20,99,30
4) 10,0,20
the answer is 4).
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thing that U need to understand First is that
Primitive Types are Passed by Values ..
Objects are passed by references but this references are passed as such by value again ...
So when any primitive values are passed to a method , any change of that value in that method is not going to get reflected to the passing method... Same applies to Reference values...
Now when u pass a Object its contents can be changed this change is reflected the passing method...
For example ,
class A {
int i = 0 ;
public static void main(String args[])
{
int j = 40 ;
A a = new A() ;
System.out.println(a.i+ " " + j) ;
passMethod(a , j) ;
System.out.println(a.i+ " " + j) ;
}
public static void passMethod(A obj , int primitive)
{
obj.i = 20 ;
primitive = 40 ;
}
}
Same explanation applies to the ur code..
Hope this clarifies ur question....
Thanks ,
Suresh R
Indy
Ranch Hand

Joined: Apr 24, 2000
Posts: 44
thanks for the reply, I pretty much understand what you were saying about value. And based on this, I can go along with the first two print out in #51. since v has been assigned by a new reference of ValHold, including v.i. so, v.i is printed out to 10. and i has been given to a new value within the method another. so, it's 0. THEN, what about the second v.i print out?
which reference it is refering?? according to the answer, it is refered, not to the original one, but the one right before v was assigned by a new reference again. How can I tell which reference it is?
Thanks in advance.
Indy
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
You might have to understand also that the v.i that is printed inside the another() method is the original v that is created within this same method. If we agree here then we should also agree that it is this same v that is passed with the call another(v,i).
when this object is passed to another, you will notice that is is changed as v.i = 20 inside the another method, and this alteration indeed does affect the original v, since no new instance of it has been created yet.So this is the 20 that is printed inside amethod.
Below this assignment there is a call to ValHold vh = new Valhold then v = vh, here now it is a completely different object since a new instance has been created through new Valhold and what happens thereafter will not affect the original v that came into this method.
I hope this will help you understand, somehow.
Indy
Ranch Hand

Joined: Apr 24, 2000
Posts: 44
YES, I got it. thank you very much.
Indy
 
wood burning stoves
 
subject: Green mock 2, #51