18

I have this code:

MyClass object;

.... some code here where object may or may not be initialised...

if (object.getId > 0) {
    ....
}

Which results in a compile error: object may not have been initialised, which is fair enough.

Now I change my code to this:

MyClass object;

.... some conditional code here where object may or may not be initialised...

if (object != null && object.getId > 0) {
     ....
}

I get the same compile error! I have to initialise object to null:

MyClass object = null;

So what's the difference between not initialising an object, and initialising to null? If I declare an object without initialisation isn't it null anyway?

Thanks

Richard H
  • 38,037
  • 37
  • 111
  • 138

3 Answers3

28
  • fields (member-variables) are initialized to null (or to a default primitive value, if they are primitives)
  • local variables are not initialized and you are responsible for setting the initial value.
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 1
    that's my question though: is "null" a value? I always it considered (perhaps naively) it "no value", hence equivalent to non-initialisation. – Richard H Nov 10 '10 at 14:59
  • 1
    Wasn't aware of that. Now I just learnt something new today as well :) – Julian Nov 10 '10 at 15:00
  • 5
    @Richard: null is very definitely a value. It's the value which doesn't refer to any object. There's a difference between "not definitely assigned" and "definitely assigned, with a value of null". – Jon Skeet Nov 10 '10 at 15:02
  • @Jon - yes the fact that I can test for null (object == null, object != null) indicates that null is a value. thx – Richard H Nov 10 '10 at 15:17
  • What does it mean to have unassigned value? what does "undefined" mean in the context of Java? I don't see how it is ever different than null? It's a null-pointer either way.. –  Feb 13 '19 at 07:18
3

It's a language-definition thing.

The language states that variables of METHOD-scope MUST be manually initialized -- if you want them to start out as NULL, you must explicitly say so -- if you fail to do so, they are basically in an undefined state.

Contrarily, the language states that variables of CLASS-scope do not need to be manually initialized -- failure to initialize them results in them automatically getting initialized to NULL -- so you don't have to worry about it.

As far as the difference between the two states (null vs. undefined), yes they are basically the same -- but the language dictates that you need to initialize a variable (whether that's done automatically for you or not, depending on the variable's scope).

Bane
  • 1,772
  • 3
  • 21
  • 28
  • 1
    @Sotti that reference you provide is specifically talking about CLASS-scoped methods, and it's absolutely correct. Re-read my post and you'll notice I distinguish between METHOD-scoped and CLASS-scoped variables. As your reference points out, Class-scoped variables do get initialized with default values; however, Method-scoped variables do not. Additionally, refer to the Java language spec, paying specific attention to "A local variable must be explicitly given a value..."-part. http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5 – Bane Aug 04 '14 at 15:16
0

Your declaration of object is really a declaration of a pointer, or reference, to an instance of MyClass on the heap. If you don't initialize the pointer you essentially get a pointer pointing to somewhere random. By explicity initializing the pointer to NULL you are setting it to point to a NULL address that the compiler knows is invalid.

Extra confusion is introduced in Java because it implicitly initialises member variables to NULL for you.

It makes a bit more sense if you've used lower level languages like C++.

brain
  • 5,496
  • 1
  • 26
  • 29
  • I don't think your assertion is correct that a failure to initialize a java-variable to NULL amounts to it pointing somewhere random. I understand what you are getting at, but I don't think it's accurate to assume that you can declare a variable without initializing it and thus obtain access to a random piece of memory. I understand that lower-level languages handle it this way, but that doesn't necessarily mean Java does it the same way. – Bane Nov 10 '10 at 15:06
  • @Bane It won't yield much to discuss which value - if any - a local variable has before assignment, because you cannot observe that value: The compiler and the bytecode verifier won't let you access a local variable before assigning it. Unless you disable the bytecode verifier and use bytecode manipulation to perform that act. – Christian Semrau Nov 10 '10 at 20:18
  • I should add that it's more likely that the Java-designers would have designed an un-initialized local-variable to automatically be assigned NULL -- except that there is no point in declaring an unassigned local variable, so it most likely indicates an overlooked programming error -- thus, they opted to generate a compile-time error to force you to deal with the issue if you attempt to read that variable. That makes more sense (than the 'pointer to random-data'-argument) given the context of Java's design-goals. – Bane Nov 10 '10 at 21:49
  • @Bane I'm not sure that having an unitialised local variable makes any more sense than an unitialised member variable. Either way I would say that it is good practice to explicitly initialise member variables. – brain Nov 11 '10 at 08:56
  • @brain: My point wasn't whether or not 1 method or the other makes 'more sense' nor what one particular programmer may or may not consider 'good practice'. My only point was that your answer was based on an assumption for which I see no evidence to assert (ie: that just because 1 language handles undefined variables 1 way, that that means Java must be handling that problem in the same way). I see no evidence to assume that an undefined variable in Java would amount to it pointing to some random place in memory -- maybe you can provide a link to something I am not familiar with??? – Bane Nov 12 '10 at 20:06
  • @Bane Technically there is no such thing as unitialized variables in Java so this discussion is a little pointless. My point was that Java makes you initialise local variables for a reason. That reason is that if a language simply extends a Stack Frame to allocate memory then the state of that memory is undefined i.e. random. Some compilers/languages may explicitly set the memory to something when they allocate it e.g. 0xDEADBEEF – brain Nov 13 '10 at 18:16