Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What does reference variable actually hold when initialized?

 
Bhavesh Sangwan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi experts ! When we create an object in java, what the actual value hplded by myclass reference...? Somebody asked me this question and I answered like - this holds the address of perticular object. But he told this is wrong answer, as java does't support pointer then how should it point some address...
So please anybody clear my confusion that what the actual process is performed by jvm here....
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your friend is being somewhat picky. The variable holds a reference, which the JVM can use to locate the object in the heap. The reference is not a physical memory address, so technically it's not a pointer. Conceptually, it's practically the same thing though.
 
Henry Wong
author
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:Your friend is being somewhat picky. The variable holds a reference, which the JVM can use to locate the object in the heap. The reference is not a physical memory address, so technically it's not a pointer. Conceptually, it's practically the same thing though.


As an implementation detail, with the Azul JVM (which I had the pleasure to work on), the lower 48 bits (I think) is a pointer to the memory address of the object. The rest of the bits is some sort of ID to confirm that it does point to a valid object. With the Sun JVM, I was under the impression that the reference did hold a memory address. Of course, this is purely an implementation detail -- the specification doesn't say that it has to be a direct memory address.

Henry
 
Bhavesh Sangwan
Ranch Hand
Posts: 57
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Friends ... I'm partially satisfy but still not sure about the actual concept.. I think someone must be sure about this concept. Yes because finally it's working somehow but HOW... HOW...?
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I understand your frustration, but a certain amount of abstraction frees you to concentrate on the things that matter, and not get lost in details. I think it's important to understand the difference between a stack and the heap, that all variables aside from primitives hold references that can be used to locate an object on the heap, and that all objects are stored on the heap. How objects are actually located using the reference value is up to the implementer of the JVM, and you understand the concept of how it works, without necessarily understanding the details.

Henry has a good point that the reference might contain the physical memory address of the object, which would be just like a pointer. However, unlike a pointer in C, for example, you can't do arithmetic on a reference to find nearby objects in the heap. For all intents and purposes, the reference is a random, but unique value that lets the JVM find your object. Many variables can hold the same reference value, in which case they are referring to the same object. An object created in a method lives in the heap and survives even after the method completes. If it was assigned to a local variable in that method, then the reference was on the stack, and is gone after the method completes. The job of the Java garbage collector is to examine the heap looking for objects that have no references either on the heap or in an active stack, and removing them. How it does that is another thing that you should understand in concept, though not necessarily in detail.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to get nitpicky about pointer vs reference then, even "pointer" is really an abstract concept. The pointer is a logical address into the virtual memory assigned to the process. The OS translates it to a physical memory address, which itself is also an abstract concept. The BIOS uses a memory controller to translate the physical memory address to the actual RAM

It's turtles all the way down. For most practical purposes, a reference is a pointer is a memory address. Everything else is details that you don't need to worry about.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic