• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference Variable

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree to Kelvin, Sandy
and finally Young .
 
zartab ameen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic