It is a well know fact that in Java one needs to initialize a local variable before using it (cf. JLS)
A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16).
Otherwise one gets a compiler error:
The local variable result may not have been initialized.
What is the rational for this design decision? Why does the compiler not automatically convert a declaration (e.g. int x
, double y
, String foo
, etc.) to a definition initialized with some default value (0, 0.0, null)? Are there any drawbacks of doing so?