+ 5

How is the default value of int and float 0 and 0.0f ;

If those are the default values then why error arises if we use the value without initializing { int a; System.out.println(a); //error : a not defined } So , how can we say that the default value of int is 0 and float 0.0f (every source tells this).

24th Oct 2017, 3:12 PM
Utkαrsh
Utkαrsh - avatar
2 Answers
+ 5
Local variables are not given their default values to avoid possible bugs. More info: https://stackoverflow.com/questions/415687/why-are-local-variables-not-initialized-in-java
24th Oct 2017, 3:20 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
@Restoring faith Thanks , That's weird , means in the class containing main method , the variables will not take their default values but in other classes they will o_O
24th Oct 2017, 3:43 PM
Utkαrsh
Utkαrsh - avatar