ahmed S
ahmed S

Reputation: 346

What is the difference between a variable assigned to null and others not assigned

what is the difference between a variable assigned to null and others not assigned in java.

I know that there is a difference in the method's block , witch mean i can use the variable that has initialised to null but i can't use that isn't initialised

can you tell me why ?

edit: Thanks to everyone who answered

My question is about the behavior and the form of references in the memory

Upvotes: 7

Views: 2286

Answers (5)

aioobe
aioobe

Reputation: 421010

What is the difference between a variable assigned to null and others not assigned?

  • An uninitialized variable does not have a value, not even null (which is why it can't be read)
  • An initialized variable does have a value (for instance null, 5 or "hello").

Note that member variables get default values assigned to them automatically (and the default value for a reference type is null). So while it may seem like you can read an "uninitialized" member variable, the truth is that it is in fact initialized.

Upvotes: 7

vathek
vathek

Reputation: 551

Directely from Oracle site

Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.

The following chart summarizes the default values for the above data types.

Data Type Default Value (for fields) byte 0 short 0 int 0 long 0L float 0.0f double 0.0d char '\u0000' String (or any object) null boolean false

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

Hope helped you!

Upvotes: 2

Maroun
Maroun

Reputation: 95968

See the JLS 4.12.5. Initial Values of Variables:

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

ReferenceType:
    ClassOrInterfaceType
    TypeVariable
    ArrayType

So there's no difference for member variables, if you don't explicitly assign it to null, it'll be implicitly assigned.

Upvotes: 1

Neeraj Jain
Neeraj Jain

Reputation: 7730

Variables assigned to null means it is not pointing to anything where as the one which is not assigned and if it is a local variable means it is not even initialized yet

Test t1=null; ----> Not Pointing to any Object 
Test t2; ------> Not even initialized 

if t2 is local variable and you will use it you'll get Compilation Error, as first initialize before use as default value is not there for this variable

Upvotes: 1

Jeff Watkins
Jeff Watkins

Reputation: 6359

It's explicit intent versus implication. If you don't set an object to anything you may have forgotten and thus the compiler reminds you. If you explicitly set to null, then the compiler is confident you made the choice. Also, for some types, the unset value could be something other than null, so allowing the compiler to make the choice is dangerous (c.f. C++ defaulting issues differing between implementations).

Upvotes: 1

Related Questions