• 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

Object References doubt

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

In this section of code from K&B p259.
I don't understand why c2 has not been made null. If it's an object and I pass its reference to a method - should the object not change? Like the ex in p204??
Thanks
=================================================
public class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}

public static void main(String[] args) {

CardBoard c2 = new CardBoard();
System.out.println(c2);
CardBoard c3 = c2.go(c2);
System.out.println(c2);


}
}
============================================
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When an object reference is sent as a parameter to a method, then a copy of the reference is sent. If you change the formal parameter to point to another object, it won't affect the original object reference.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith has highlighted a very interesting point.

Although you can change the properties of an object passed as argument, you cannot consider it's passed by reference. As Keith said, if you change the argument to null for example, the original variable will not be affected.

Recently I faced the following question.

Which of the following affirmations are true when passing a method argument :

a) primitive types are passed by value correct
b) array and object types are passed by reference
c) array and object types are passed by value correct
d) array and object types are immutable
e) all of above
f) none of above
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My understanding is as follows:

When you call the go() method, it creates a new reference called cb which points (refers) to the object specified in the parameter.

In this case the object is the same object referenced by c2.

When you set cb to null, all you are doing is getting the reference (cb) to point to nothing. You are not changing the object - you are only changing the reference. c2 will still happily point to the Cardboard object created in the main method. Remember using "=" and calling a method on an object do different things. "=" usually acts on the reference, whist methods act on the object. Saying ref = null does NOT destroy the object. All it does is makes the reference "ref" point to nothing. Also remember that cb and c2 are not the same reference. cb is another reference which just has the address (the location of what c2 points to) copied over.

When the method ends, it returns null. This null value is assigned to c3. c2 however has not been changed, because the method go did not modify the object. All go() did was to get one of the references pointing to it to "look" elsewhere.

Sometimes it's a bit hard to explain this without diagrams. Reading K&B sections on garbage collection may help.

It's a good idea to thoroughly grasp the concepts of references as opposed to actual objects. You will almost certainly get a question on garbage collection which tests this out.

good luck
 
Tamara Lopez
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get it now! Thank you all! That the attributes can change was confusing me.
[ March 28, 2006: Message edited by: Tamara Lopez ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have question..
can anybody help me out...

Given:
1. public class NewGarb {
2. public static Object getIt() {
3. Object rg = new Integer(3);
4. Object dg[][] = new Object[1][2];
5. dg[0][1] = rg;
6. dg[0][0] = rg;
7. rg = null;
8. return rg;
9. }
10. }

Which statement is true?


(1) The NewGarb class will not compile
(2) The getIt() method must not be declared as static
(3) The NewGarb class compiles, but an exception is received because dg is not set to null
(4) The rg object is eligible for garbage collection after a call to the getIt() method has returned
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to me 4 is true....
 
ugayatri gupta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how is it 4 ..can u please explain it..
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say 4 also, the thing is that the class is totaly legal and while in getIt() you make an object and than set it to null, when something is set to null it is available for GC
 
Phil Kurian
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when something is set to null it is available for GC



Don't mean to be pedantic, but this is not always true.

Setting to null will make an object available for GC provided the object has no other references to it from live threads.

In the case of this example, prior to setting rg=null, the object created by "new Integer(3)" had 3 references pointing to it, namely

rg
dg[0][1]
dg[0][1]

Now when rg gets set to null, the object (new Integer(3)) still has 2 references pointing to it. So whilst still in the method, the object is not available for GC.

The object however does become available for GC after the method exists. This is because the dg array is local to the method. When the method exits, the dg references get removed from the stack and thus the object (new Integer(3)) has no live references anymore.

Note that if rg had not been set to null, the rg references would also have been removed when the method exits. However the object would not necessarily be available for GC because the reference was being passed back as a return object - which could be assigned to a live reference.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think question 4 is poorly stated.

If they mean the object originally referred to by rg, then it should be eligible for garbage collection when the method exits, since rg was pointed to null, and all other references to the original object are within the method.
reply
    Bookmark Topic Watch Topic
  • New Topic