When we define a class that has members of the base data type in the class, but does not initialize them,
This time, if we use this variable, Java will initialize them by default.
Note: Java only initializes the base type members in the class, and if the basic data type is not initialized in the method,
Compile at this time will be an error. (c, the report will be reported to the police in C + +).
To avoid unnecessary problems, it is advisable to manually initialize all data types and objects at design time.
The following table shows the default values for the base type.
Java default initialization of the base data type
Basic type |
Default value |
Boolean |
False |
Char |
' \u0000 ' (space) |
Byte |
(byte) 0 |
Short |
(short) 0 |
Int |
0 |
Long |
0L |
Float |
0.0f |
Double |
0.0d |
Here is a simple validation Java execution default initialization program:
*
* Thinking in Java Test 2.1
* @author: Wolfofsiberian */public
class tij_test2_1{
Boolean B;
int i;
char c;
public static void Main (string[] args) {
tij_test2_1 test=new tij_test2_1 ();
System.out.println ("Boolean B is:" +test.b);
System.out.println ("Integer i is:" +test.i);
if ('% ' = = test.c && ' \u0000 ' ==test.c && ' = = ' = ' \u0000 ')
System.out.print ("Character C is:" + test.c+ "!");
}
Execution results:
Boolean b Is:false
Integer I is:0
Character C is:.