Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many bytes a null object? [duplicate]

When we have a reference variable e.g. to simplify it Integer i we can assume that the size of i is approximately 16 bytes overhead + 4 bytes for the actual int + 4 bytes padding i.e. 24 bytes.
So my question is if i is null do we only have 4 bytes of the reference not pointing anywhere or is there any other extra "hidden" information adding to this?

like image 933
Jim Avatar asked Sep 27 '22 23:09

Jim


2 Answers

If I recall correctly, the JVM specification mentions that null is not required to have a specific value or representation, so it could be anything. What it typically is, I don't know, but a fair guess is probably 32 or 64 bits of zeros, depending on the JVM version.

Edit: Here's the relevant section: http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.4

The Java Virtual Machine specification does not mandate a concrete value encoding null.

Cheers,

like image 56
Anders R. Bystrup Avatar answered Oct 05 '22 08:10

Anders R. Bystrup


Yeah it still uses memory. Uses 4 bytes for a 32bit and 8 for a 64 I believe.

found more information on this: Java - Does null variable require space in memory

like image 24
0x2B Avatar answered Oct 05 '22 08:10

0x2B