public class AboutBoolean {
public static void main(String[] args) {
testBoolean();
}
//注意这里必须放在外边(类加载 默认给赋值,
//如果放在方法内部直接报错 Variable 'min' might not been initialized 表示没有初始化)
static boolean min;
static Boolean max;
public static void testBoolean(){
//min的默认值是: false 因为min是false
if(min){
System.out.println("min的默认值是: "+min);
}else {
System.out.println("min的默认值是: "+min);
}
System.out.println("-------------");
//Exception in thread "main" java.lang.NullPointerException 因为max是null
if(max){
System.out.println("max的默认值是: "+max);
}else {
System.out.println("max的默认值是: "+max);
}
}
}
总结:如果是全局变量,包装类Boolean是会被默认赋值为null,而基础类型boolean会被默认赋值为false的。赋值过程应该是在 类加载的时候赋值的。
如果是局部变量,当你不赋值去使用的时候,编译器会直接报错,所以局部变量肯定是没有默认值的。