Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Pointer to Pointer

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

I earlier had an understanding that when we create an object, the object is created on a heap and the reference we create, which points to the object is on the stack.

But recently I read something like: the reference we create does not directly point to the object in the heap, but it points to another memory location on the stack which has the reference to the object on heap.

I am a bit confused So please help !!

Thanks in advance

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

The content of reference variable depends of current JVM. So, it might be a pointer to object or pointer to pointer. What's the diff?

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

Originally posted by Ankur Srivastava:

But recently I read something like: the reference we create does not directly point to the object in the heap, but it points to another memory location on the stack which has the reference to the object on heap.



I don't feel any advantage of having double reference, so logically it should not be like this. Could you post link or book name where you have read this...
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't feel any advantage of having double reference, so logically it should not be like this.

The Java� Virtual Machine Specification:

In some of Sun's implementations of the Java virtual machine, a reference to a class instance is a pointer to a handle that is itself a pair of pointers: one to a table containing the methods of the object and a pointer to the Class object that represents the type of the object, and the other to the memory allocated from the heap for the object data.
 
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

Originally posted by rathi ji:

I don't feel any advantage of having double reference



Double pointers like this (often called "handles") are a common feature of automated heap managers. I remember that the old Mac Toolbox used handles for everything. The useful thing about handles is that you can compact the heap by moving objects around to fill in empty space, and the pointer held by the application doesn't have to be changed -- only the "middle" pointer changes when a memory block is moved, while the application pointer still points to the "middle" pointer.
 
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems like a basic jump table to me. By using double-indirection, the JVM can optimize memory by moving objects around and it would only have to update the target side of the table.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic