1

When I declare an instance variable, such as:

BSTNode node;

I know that node is now null, since the instance variable is not initialized. But is some memory allocated to store the value of null, and is node now a reference to that location?

I tried using

System.out.println(node);

Hoping that I would see the address of the reference, but just saw "null" printed out. Why do I not see an address?

PuffySparrow
  • 897
  • 3
  • 10
  • 23

4 Answers4

3

Instance variables are initialized to their default values, if the code doesn't explicitly initialize them. All reference variables are initialized to null, per JLS Section 4.12.5:

Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):

...

For all reference types (§4.3), the default value is null.

When a null is passed to System.out.println, the string literal "null" is outputted. The Javadocs for println defer to the print method to cover what happens when a null is passed:

If the argument is null then the string "null" is printed.

There is no address because there is no object yet.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Bohemian states that memory is still allocated, so would the memory location be referred by the variable initialized with a default value of null? – PuffySparrow Feb 05 '14 at 00:39
  • Yes, memory is still allocated for the reference variable itself, but not for any object. The value `null` is a Java construct that is treated specially. Think of it as a special value that refers to no object / no memory location at all. – rgettman Feb 05 '14 at 01:04
1

Yes, memory is allocated and a value assigned no matter the initialization.

There is no such thing as "uninitialized": there's explicitly initialized, or initialized with the default value.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Is there a way I could find the location of a variable that has been initialized with the default value of null? – PuffySparrow Feb 05 '14 at 00:37
  • 1
    What do you mean, the "location"? It's a field stored in the object of the enclosing class. – Louis Wasserman Feb 05 '14 at 00:38
  • 1
    Java doesn't use "memory addresses" for variables like C does. The value could be anywhere in memory and may move around as the JVM see fit. The variable is an abstraction of the real memory location, which is not accessible to code. – Bohemian Feb 05 '14 at 00:41
  • If java doesn't use "memory addresses", what is it that I see when I use System.out.print(someObject)? I always thought that was a memory location? – PuffySparrow Feb 05 '14 at 00:44
  • 1
    It's the identity hash code. See [`System.identityHashCode(Object)`](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#identityHashCode(java.lang.Object)) – Bohemian Feb 05 '14 at 00:57
0

Java has no pointers like C/C++. You will never See the memory pointer where you object is located. And if you would get it, it might even chance as garbage collection might move it.

If you print an object, it is made a string calling toString().

brummfondel
  • 1,202
  • 1
  • 8
  • 11
0

"Is node now a reference to that location?"

How is null represented in the memory?

That is implementation specific, and you won't be able to see the representation of null in a pure Java program. (But null is represented as a zero machine address / pointer in most if not all Java implementations.)

There is already a very detailed answer to this question in Stackoverflow: You should check it out!

Also watch this video by Tony Hoare, "Null References: The Billion Dollar Mistake".

Community
  • 1
  • 1
David Rutgos
  • 149
  • 2
  • 12