3

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).

Shog9
  • 156,901
  • 35
  • 231
  • 235
The Student
  • 27,520
  • 68
  • 161
  • 264

5 Answers5

7

See here:

The Java IAQ: Infrequently Answered Questions: Is null an Object?

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • excelent link! Thanks! (I would like to hear more opinions before choose as right) – The Student Sep 24 '10 at 21:00
  • 1
    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 Sep 24 '10 at 21:51
  • 3
    @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 Sep 24 '10 at 21:52
  • Well, unless you think it's *still* borderline, a link without explanation is still inappropriate. Not downvoting, but also not upvoting. – Steven Sudit Sep 25 '10 at 01:45
3

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
  • 19,391
  • 1
  • 51
  • 53
  • 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? – The Student Sep 24 '10 at 21:02
  • The only difference is that the compiler noticed that you used the value before ever initializing it, so it complained. – Steven Sudit Sep 24 '10 at 21:03
  • @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ý Sep 24 '10 at 21:06
  • @Peter but you must agree that **null** is not exactly a value.. it's the "no value" literal. So, **null** could exist somewhere.. – The Student Sep 24 '10 at 21:10
  • @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 Sep 24 '10 at 21:47
  • @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 Sep 24 '10 at 21:48
  • @David: Whatever it points to, if anything, it's not a valid object. – Steven Sudit Sep 24 '10 at 21:50
  • @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ý Sep 25 '10 at 07:42
  • @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 Sep 25 '10 at 16:19
3

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
  • 203,806
  • 29
  • 246
  • 1,045
0

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
  • 44,725
  • 9
  • 65
  • 93
0

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. :-)

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138