Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Why am I required to initialize a primitive local variable?

Tags:

java

primitive

public class Foo {     public static void main(String[] args) {         float f;         System.out.println(f);     } } 

The print statement causes the following compile-time error,

The local variable f may not have been initialized

If primitives in Java already have a default value (float = 0.0f), why am I required to define one?


Edit:

So, this works

public class Foo {     float f;     public static void main(String[] args) {         System.out.println(new Foo().f);     } } 

Thanks, everyone!

like image 638
user1329572 Avatar asked Jun 22 '12 23:06

user1329572


People also ask

Why initialization of local variable is mandatory?

Local variables and primitives should be initialized before use because you would know what to expect from the values. Historically, when a new variable was created it would contain random values from the memory [and one could not predict the value].

Is it mandatory to initialize variables in Java?

Variables in Java can be categorized into instance, static & local. In programming, it is always advisable to only declare and initialize a variable right before it's usage.

What happens when local variable is not initialized in Java?

Should we declare a local variable without an initial value, we get an error. This error occurs only for local variables since Java automatically initializes the instance variables at compile time (it sets 0 for integers, false for boolean, etc.).

What will happen if you don't initialize a local variable and try to print it?

If a variable is declared but not initialized or uninitialized and if those variables are trying to print, then, it will return 0 or some garbage value.


2 Answers

Because it's a local variable. This is why nothing is assigned to it :

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.

Edit: Why does Java raise this compilation error ? If we look at the IdentifierExpression.java class file, we will find this block :

... if (field.isLocal()) {             LocalMember local = (LocalMember)field;             if (local.scopeNumber < ctx.frameNumber && !local.isFinal()) {                 env.error(where, "invalid.uplevel", id);             }             if (!vset.testVar(local.number)) {                 env.error(where, "var.not.initialized", id);                 vset.addVar(local.number);             }             local.readcount++;         } ... 

As stated (if (!vset.testVar(local.number)) {), the JDK checks (with testVar) if the variable is assigned (Vset's source code where we can find testVar code). If not, it raises the error var.not.initialized from a properties file :

... javac.err.var.not.initialized=\     Variable {0} may not have been initialized. ... 

Source

like image 151
Zakaria Avatar answered Sep 29 '22 06:09

Zakaria


In fact, the compiler does not assign a default value to your float f, because in this case it is a local variable -- and not a field:

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.

like image 24
sarnold Avatar answered Sep 29 '22 07:09

sarnold