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?
So, this works
public class Foo { float f; public static void main(String[] args) { System.out.println(new Foo().f); } }
Thanks, everyone!
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].
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.
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.).
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With