tags:

views:

252

answers:

5

In Java, is null an existing object on the heap?

I'm trying to understand the difference between an uninitialized local variable (what does not compile) and one initialized with null (what compiles).

+6  A: 

See here:

The Java IAQ: Infrequently Answered Questions
http://norvig.com/java-iaq.html

Do a search on the page for "Is null an object". It's about halfway down the page.

Robert Harvey
excelent link! Thanks! (I would like to hear more opinions before choose as right)
Tom Brito
Here’s a direct link: http://norvig.com/java-iaq.html#null
Gumbo
The only reason I'm not upvoting this otherwise correct answer is because I thought the goal was to answer here and link to supporting documentation and reference. This is just a link.
Steven Sudit
@Steven: The question (as originally asked) was borderline, and it's been closed once already. I was just trying to point the OP in the right direction.
Robert Harvey
Well, unless you think it's *still* borderline, a link without explanation is still inappropriate. Not downvoting, but also not upvoting.
Steven Sudit
+3  A: 

Or, to save you the clicking, the answer is no, it is not an object. Being null means that no object is referenced.

Steven Sudit
so, what's the difference between null and uninitialized? If a local var is not initialized it will not compile... but it is the same as null, so why it don't compile?
Tom Brito
The only difference is that the compiler noticed that you used the value before ever initializing it, so it complained.
Steven Sudit
@Tom: uninitialized means there is no defined value in it, so it may be random, and you cannot really use it until you give it some value.
Peter Štibraný
@Peter but you must agree that **null** is not exactly a value.. it's the "no value" literal. So, **null** could exist somewhere..
Tom Brito
@Tom Brito: `null` is guaranteed to be invalid, no matter what. In C and C++, it's often represented by an all-bits-zero pointer. Technically, it points somewhere, but it's usually protected memory, so you couldn't use what it points to anyway.
David Thornley
@Peter: I know it's not Java, but in C#, an uninitialized reference value actually contains null. This is done because the CLR is not willing to allocate blocks of memory and hand them over without zeroing them out. I suspect it is much the same in the JVM.
Steven Sudit
@David: Whatever it points to, if anything, it's not a valid object.
Steven Sudit
@Steven: probably yes, although it's hard to verify since Java won't let you use uninitialized local variable. Fields within class are however initialized to their default values (null/false/0).
Peter Štibraný
@Peter: Interesting. It may well be that the heap initializes with zero, but the stack is kept clean by the compiler ensuring that variables are explicitly initialized before use.
Steven Sudit
+3  A: 

This question highlights the connection between scope and definite assignment. If you step through the code below in a debugger, you can see several features as you break on each line:

  1. No breakpoint possible; i is not in scope.
  2. i is in scope, but "not a known variable in the current context."
  3. The reference i has been assigned, and it has the value null.
  4. The reference i has the value 1, which will be incremented after line 4.

Listing:

Integer i;
i = null;
i = Integer.valueOf(1);
i++;

Addendum:

So, what's the difference between null and uninitialized?

Prior to definite assignment, a local variable's value is inaccessible; any attempt to access the value is rejected at compile-time. Once a definite value is assigned, even null, that value can be accessed. This is a critical security feature in Java. In some languages, an uninitialized variable may reference values in memory left by a previous process.

Addendum: The content of a frame's local variables prior to initialization is unspecified. The bytecode verifier precludes bytecode that accesses uninitialized memory, but deviant bytecode is possible.

trashgod
Ok, but what's actually at that memory location before the initalization? Is it zeroed out?
Steven Sudit
I believe the process is implementation specific; more above.
trashgod
A: 

a local variable that hasn't been assigned yet, probably points to null too. I'm not a JVM expert, but that seems to be the logical choice.

so compiler checks for you to make sure you assigned something to the variable. other than that there's no difference. at runtime local variables not initialized yet are assigned to null.

irreputable
Yes, you're probably correct.
Steven Sudit
It doesn't 'point to' anything. Having the *value* null is *instead of* pointing to something.
EJP
That's too small a nit to justify a downvote.
Steven Sudit
I disagree completely. This is the essence of the original question. The answer is incorrect.
EJP
A: 

Null is NOT an object. In fact it is the opposite of an object, and why you can't make method calls against a reference pointing at null. If you really must know what null is you can think of it as a something like zero. In fact a reference that points to null doesn't take up anymore memory, ie. zero. It has no value so it's a reference that doesn't refer to any object.

In Java you have to initialize a variable before you can use it. For reasons of history Java doesn't want you to assume values so the compiler forces you to assign a value to it. Part of the reasons for this is the bugs and security problems that C caused because it didn't initialize values or force you to do it before they were used. Java does initialize some values for primitives, and instance variables: 0, 0.0, false, and null etc, but for local variables you have to give it a value. It's for your own protection. :-)

chubbard