Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Head First Java Third Edition: What's the Analogy for Reference Variable and Object Reference?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi community,

I have a question on the analogy for object reference and reference variable in chapter 3. Under the heading "Controlling your Dog object", it's mentioned that we can think of a Dog reference variable as a Dog remote control. In the "Bullet Points" section also it's mentioned that we can think of a reference variable as a remote control.

At the same time, in the same chapter, there's a heading as "An object reference is just another variable value". In there, it's mentioned as object reference is the remote control. In the "Java Exposed" section in the same chapter, the object reference says that it's a remote control.

Earlier we studied that variables are just cups. So, I thought of reference variables as cups, reference variables hold object references and object references are the remote controllers. Please help me to clear the doubt. What is the analogy for reference variable and object reference?

Thank you.
 
Marshal
Posts: 76110
362
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is a case of mixed metaphors. Metaphors are useful but they are usually not a 100% complete description of what they represent. Yes, a variable is a way to get access to a value, and that can be used like a remote control, but only to a reference value. In the case of a primitive, the memory location contains the actual value, and the space occupied differs from type to type; an int occupies 4 bytes and a long occupies 8 bytes. That is like having different cup sizes. But in the case of primitives, all you can do is empty the cup and refill it with something else (=assigning to a new value).
You can empty the cup for a reference variable (assign to null) and refill it (assign to something else). Whatever is in the cup acts as a remote control because you can use that value (it allows access to the memory location of the object) to do something else. Reassigning that reference variable is like getting a remote control to a different television.
 
Thanoshan Vijayanandan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the detailed explanation of primitives and references, Ritchie.

Whatever is in the cup acts as a remote control because you can use that value (it allows access to the memory location of the object) to do something else.



After reading your message, I thought some points on the reference variable maybe are wrong. They are:
  • Whatever in the reference cup acts as a remote control because we can use that value for an object to do something.
  • I think always a reference variable contains a remote control because what are the values a reference variable can have in the worst case? 1. Reference to object (Object Reference) 2. Reference to null value (Null Reference)
  • If object reference, the remote control is programmed to control a TV
  • If null reference, the remote control is not programmed to control any TV
  • Reassigning a reference variable to another object (changing the reference to another object) is like reprogramming the remote control to control different TV
  • Reference variable (reference cup) contains a reference (remote control) always. Reference can be object reference and null reference.
  • Therefore, we can use the reference variable (using the reference - remote control) for an object to do something, if the reference is an object reference. Otherwise, if the reference is a null reference, using the reference variable is like pressing the buttons of the remote control that is not programmed to control any TV - nothing happens
  •  
    Campbell Ritchie
    Marshal
    Posts: 76110
    362
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Thanoshan Vijayanandan wrote:Thank you for the detailed explanation . . .

    That's a pleasure

    . . .

  • I think always a reference variable contains a remote control because what are the values a reference variable can have in the worst case? 1. Reference to object (Object Reference) 2. Reference to null value (Null Reference)
  • I am afraid I disagree. Since null means no object, that is more like absence of a TV. [At this point the analogy starts to break down completely.] Remember any attempt to use a null as if it were an object will cause problems (an exception).

    . . .

  • If null reference, the remote control is not programmed to control any TV
  • No, that is more like the TV being absent. It can't even be found down the back of the sofa.

    . . .

  • . . . Otherwise, if the reference is a null reference, using the reference variable is like pressing the buttons of the remote control that is not programmed to control any TV - nothing happens
  • No, as I said, it fails to contact anything and an exception is thrown.
     
    Thanoshan Vijayanandan
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Ritchie. Now, I'm able to understand object reference and null reference as well. You explained them very nicely. I've clearly understood. Thank you
     
    Campbell Ritchie
    Marshal
    Posts: 76110
    362
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's a pleasure
    reply
      Bookmark Topic Watch Topic
    • New Topic