5

If I declare some string variable like

String str = null; 
  • Is there memory allocated for variable str?
  • If allocated, how many bytes will be allocated?
  • If not allocated, how does the JVM know that there is some variable called str been declared?
  • If allocated and if there is null value inside the memory, then what exactly is the representation of null in binary?
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Divakara SR
  • 51
  • 1
  • 2

5 Answers5

9

When you do something like:

String str = null;

The only thing allocated is a reference to a string (this is analogous to a string-pointer in languages which contain pointer-types). This reference is a 32 or 64 bit variable (depending upon your hardware architecture and Java version) which resides on the stack (probably, depending upon the exact context in which your declaration is placed).

No memory should be allocated for the null itself, because null is not actually a valid object instance. It is simply a placeholder that indicates that the object reference is not currently referring to an object. Probably it is simply the literal value 0, such that assigning an object reference to null is equivalent to setting a pointer type to NULL in C/C++. But that last bit is conjecture on my part.

aroth
  • 54,026
  • 20
  • 135
  • 176
3

Null is the value of a reference that is not bound to an object.

Space is allocated for the reference variable itself, regardless of its value. Null is merely a value that fits within this space.

To answer each question for the specific example.

  • Yes, there is space allocated for the variable str.
  • The size of the memory allocated for the reference variable is probably four bytes.
  • It is allocated, and the JVM follows the instructions of the compiled code to access it.
  • It doesn't matter.
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
2

Take a look at this Java IAQ (infrequently asked question) "Is null an object?".

To answer your questions:

  • Only memory to store the reference is allocated
  • The java standard doesn't specify how much memory a reference takes up (although, in practice it is usually 32 or 64 bits depending on your JVM - although some JVMs try to beat this)
  • The memory representation of null doesn't matter, since you can't do bit level operations on it (or assign it to anything that isn't a reference)
Timothy Jones
  • 21,495
  • 6
  • 60
  • 90
1

Java works on references. Think of String str=null as you have pointer in C, but not exactly pointer.

Now internal representation dependes upon the JVM. It is not easy to calculate the size of reference. The implementation differs per JVM so there is no point extracting that low level info.

havexz
  • 9,550
  • 2
  • 33
  • 29
0

After doing this line:

String str = null; // or String str;

Only a reference of type String is allocated into the stack. null means that you don't assign any value (object) to the reference str into the heap.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417