Default Value of local variables
I read several questions from Java certified engineers yesterday. One question is as follows.
The default value of the local variable Boolean is
A, false
B. True
C. null
D. No default value. Initialization is required.
The correct answer is D. At that time, my answer was a false. Why should I select a? The reason is as follows.
Default Value of basic type
Boolean false
Char '\ u0000' (null)
Byte (byte) 0
Short (short) 0
Int 0
Long 0l
Float 0.0f
Double 0.0d
However, my answer is wrong. I typed a similar code in eclipse manually, tested it, and proved again that the answer D is correct. The specific reasons are as follows.
The system does not automatically assign initial values to local variables, but the system automatically assigns initial values to member variables. The value of the basic type is 0, and the value of the composite type is null. Therefore, the default value of the above type is only used for member variables and not for local variables.
Last modified: 2011-12-25
For more technical articles, visit thinkblog.