• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

What is actual value stored in a reference variable referring an object

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question may be very funny.. but someone asked me that what the value stored in reference variable when we refer some object to it...like -

Test obj = new Test();

now what is the value in "obj" variable....and how much memory it will consume...?
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A reference variable "obj" contains an address, or a reference to an address (similar to pointer variables in C++). A reference variable provides direct access to this memory address.
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value stored in the reference variable is the house no.(or address in some format) of the variable, so that anyone can go and talk to the object variable. As per my information reference variable consume 2 bytes.
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The size of a reference isn't well defined, but it is typically 4 bytes on a 32-bit system and 8 bytes on a 64-bit system.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Kumar Jain wrote:As per my information reference variable consume 2 bytes.



Definitely not two bytes! It depends entirely on the implementation, of course, but a 32-bit pointer is (of course) 4 bytes, and a 64-bit pointer is (of course) 8, so, depending on your architecture, these are likely values for the size of a reference variable.
 
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
How exactly the reference to an object is stored in a reference variable, is not specified by the Java Language Specification, and not even by the Java Virtual Machine Specification. It's left up to whoever creates a JVM, so that that person can choose whatever works best for the target platform he or she is creating a JVM for.

A logical choice would be the address of the object in memory (like a pointer in C), and that's probably what Oracle's JVM does.

There's no way in Java to directly see what the value of a reference variable is (i.e., there's no way to see what the reference value itself looks like).

Manoj Kumar Jain wrote:As per my information reference variable consume 2 bytes.


If it's stored as a pointer, it's going to be either 4 bytes (on a 32-bit system) or 8 bytes (on a 64-bit system).
 
Bhavesh Kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my answer was also pointing to these similar answers..but I don't know what the person- who asked me this question, was expecting to me.... and he declared it - WRONG.

I become nervous and so took help from all of you geeks...

According to him - Java does not support pointers .. so there would be some another concept and what is that concept...

Surely anyone would know the exact answer to satisfy....

Please help.......
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bhavesh Kumar wrote:According to him - Java does not support pointers .. so there would be some another concept and what is that concept...



It doesn't support pointers in the sense that you can't do pointer arithmetic and you can't access arbitrary areas of memory. But that doesn't stop the JVM using pointers behind the scenes to implement a reference, and it doesn't mean it isn't a similar concept.

I usually think of a reference as a restricted pointer. You can only have a reference value by creating a new object or copying an existing reference value. How they behave is the important thing, not what the underlying mechanism is (unless you're writing a JVM of course!).
 
Bhavesh Kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Matthew ! I agree with your statement....

but then what is the value when we generally print any object on the console like ...

Test obj = new Test();

and then...

System.out.println(obj);

and it prints something like - package.classname@something


then what is "something" here....? some memory address..? or something else....?

so confusing...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What gets printed then is whatever the toString() method of that object returns. So you aren't printing the reference, you're calling a method and printing the output.

If toString() hasn't been overridden, it uses the version in java.lang.Object. And if you look at the Javadocs for that, you'll see it says the something is the hashCode(). And if you look up that, you'll see it says:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)



So you're probably seeing something that was calculated from the memory address. But not the actual memory address - there aren't enough integer values!
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the documentation for java.lang.Object#toString()
 
Bhavesh Kumar
Greenhorn
Posts: 18
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh thanks Matthew! Thanks a lot .....

Now I got my answer.. Now I can bet on my answer...


Thanks and JavaRanch is a wonderful platform for Java Developers
 
What are your superhero powers? Go ahead and try them on this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic