wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reference Variable" Watch "Reference Variable" New topic
Author

Reference Variable

zartab ameen
Greenhorn

Joined: May 01, 2007
Posts: 13
Hi i have confusion in the following in the following program

public class BettyAck {
public static void main(String argv[]){
BettyAck b =new BettyAck();
}
public BettyAck() {
Integer x = new Integer(10);
findOut(x);
Integer y = new Integer(99);
Integer z = y;
z = null;
findOut(y);
//here
}
public void findOut(Integer y){
y = null;

}
}

OPTIONS
0
1
2
3
4

i gussed the answer was 3 but the answer is 0,can you give me the explanation
THANK YOU
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
The program is incorrect and has compile time errors.
I modifed it as below. But there arent any System.out.println() statemnts, So what is the output that you are expecting from this code.



Thanks
Deepak


SCJP, SCWCD, SCBCD
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
I'm guessing that the question was "How many objects are eligible for garbage collection at the line marked 'here'?"

The key thing to realize here is that when you pass a reference as a method argument, the method only receives a copy of the reference. Changing the parameter variable in the method body does not change the original variable used by the caller. In other words, the following program fragment should print "5".

[ November 27, 2007: Message edited by: Kelvin Lim ]

SCJP 5.0
Sandip Sarkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 55
JUST TO ADD ON TO THIS THREAD.....

DEEPAK THE PROGRAM COMPILES FINE AS POSTED BY THE AUTHOR OF THIS POST.
I DONT SEE ANY REASON WHY THAT SHOULDNT GET COMPILED.
YOU CAN HAVE IT CHECKED BY YOUR JVM.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by sandy:
DEEPAK THE PROGRAM COMPILES FINE AS POSTED BY THE AUTHOR OF THIS POST.
I DONT SEE ANY REASON WHY THAT SHOULDNT GET COMPILED.
YOU CAN HAVE IT CHECKED BY YOUR JVM.

Agreed. I don't see any problems with the original code either.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

"sandy", please check your private messages. You can see them by clicking My Private Messages.

And please don't write in ALL CAPITALS. It looks as if you are SHOUTING.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
I agree to Kelvin, Sandy
and finally Young .
zartab ameen
Greenhorn

Joined: May 01, 2007
Posts: 13
Yes you guessed it correct the question is how many reference variable are eligible for garbage collection.

I guessed it was 3 but the correct option is 0.

From K&B book page 204 when you are passing reference variable you are actually passing the bit pattern that means when you do some thing in a method the value will be also reflected outside the method,similarly when you assing null the other reference also becomes null.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by zartab ameen:
From K&B book page 204 when you are passing reference variable you are actually passing the bit pattern that means when you do some thing in a method the value will be also reflected outside the method,similarly when you assing null the other reference also becomes null.


No, that's not the correct interpretation of that statement. You are passing a copy of the value of the reference, but not the reference itself. Perhaps you'll find this JavaRanch article on pass-by-value semantics helpful in understanding what actually happens.
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Hi,
It was meant that changes made to the object will be visible across methods and not changes made to the reference variable.


Here a instance of TestB() is created and asssigned to the reference variable ref. Now using ref I invoke changeMe() and pass the same ref variable to the method. Using the ref variable inside the method i modify the value of a which is reflected after the method completes. Also inside the method i assign null to ref. Even after assigning null to ref , I am still able to invoke getMe() and changeMe() after line 1.
This shows that reference variables/any variables that are passed to the method are all ways passed by value, i.e., a copy of the value contained in the argument is passed as a parameter to the method.
I hope this clears your doubt. If it does not try executing the examples in K&B again and read the concept again. Its pretty simple.
Thanks
Deepak
Julio Eneriz
Greenhorn

Joined: Nov 28, 2007
Posts: 15
Originally posted by zartab ameen:
Yes you guessed it correct the question is how many reference variable are eligible for garbage collection.

I guessed it was 3 but the correct option is 0.

From K&B book page 204 when you are passing reference variable you are actually passing the bit pattern that means when you do some thing in a method the value will be also reflected outside the method,similarly when you assing null the other reference also becomes null.


Why 3? I should have gone for 2...

By the way, is there a way to test it? I'm trying to code as much as possible, but as the GC invocation is not guaranteed, I don't see a way to do it for this example.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reference Variable