The Java language requires that variables follow the rules that are first defined, then initialized, and then used. The initialization of a variable is the first time it is assigned an initial value since the definition of the variable.
First, member variables
The JVM assigns default values (the default) to the instance and static variables of the class, including each element in the array arrays--without having to write the initialization assignment statement. The final variable has no default value and must be assigned before the constructor ends.
The default values are as follows:
1, the integer type (byte, short, int, long) is the default value of 0.
2. The default value of single-precision float (float) is 0.0f.
3, double-precision floating-point type (double) default value is 0.0d.
4, the character type (char) defaults to "/u0000".
5, Boolean default value is False.
6, the reference type default value is null.
7, the array reference type default value is null. If no values are displayed for each element, Java initializes all elements of the array to their corresponding type's default values.
This default assignment is done before the object's constructor call, if the program writes an assignment to the instance and static variables, and the value given is the default value of the JVM, then it is undoubtedly superfluous and repeated work again.
Situation One:
public class Foo { private int count=0; Redundant private static Boolean dd=false;//Extra Public Foo () { super (); } }
situation Two:
public class Foo { private int count; private static Boolean DD; Public Foo () { super (); count=0; Excess dd=false; Superfluous } }
second, local variables
A local variable has no default value and must be initialized manually!
If the compiler confirms that a local variable may not be initialized before it is used, the compiler will make an error.
Note: If the local variable is not initialized and has not been used in the method, both the compilation and the Run pass.
public void Method () {int a;a++;//compile error, variable a must initialize System.out.println (a);}
Java Variable default value