• 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

Size of reference in Java?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all

what is the size of reference variable in Java ?

eg


what is the size of foo in my case ?
JVM is 32bit.

what i understand is ...

if JVM is 32bit , reference variable is of 4bytes since (8*4=32bit)
if JVM is 64bit , reference variable is of 8bytes since (8*8=64bit)

is my answer correct ??
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is "it doesn't matter".

I am not sure if the size is specified in the JLS (i took a quick look and didn't see it), so each implementation of the JVM could be free to do it however they want.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can just imagine somebody deciding that a reference used twice as much memory in a 64-bit JVM as in a 32-bit JVM, and then declaring that to be a problem. Which of course it isn't; a 64-bit machine allows you to address 4 billion times as much memory as a 32-bit machine does.
 
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

naved momin wrote:what i understand is ...

if JVM is 32bit , reference variable is of 4bytes since (8*4=32bit)
if JVM is 64bit , reference variable is of 8bytes since (8*8=64bit)


It's not that simple. The Java Language Specification and the Java Virtual Machine Specification don't specify what the size of a reference is. That is left up to the particular implementation of the JVM. It's not something you normally need to know when you write software in Java.

On Oracle's JVM, it's not as simple as 4 bytes on 32-bit and 8 bytes on 64-bit. One of the optimizations that Oracle's JVM has is compressed pointers; which means that under certain circumstances, references on the 64-bit JVM are only 4 bytes, not 8 bytes. You can find some more details on that here: Java HotSpotâ„¢ Virtual Machine Performance Enhancements. This is definitely not a beginner's subject, and definitely not something that you need to know unless you're working on the low-level details of the JVM.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the answers...
i asked because when i was reading a placement crack book ...they used pointer size as well to determine the optimality of the code for a given problem .....
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic