为什么对布尔Java的默认值设置为true? [英] Why is Java's default value for Boolean set to true?
问题描述
为什么私人布尔shouldDropTables;
分配真正
默认为变量,而不是 NULL
,写私人整数anInteger时等;?
Why does private Boolean shouldDropTables;
assign true
by default to the variable instead of NULL
, like when writing private Integer anInteger;
?
问,因为我碰到一些code,那里有一个 shouldDropTables
布尔变量作为一个评价来 NULL
与否确定是否执行的方法。
Asking because I came across some code where there was an evaluation on a shouldDropTables
boolean variable being NULL
or not determining whether to execute a method.
推荐答案
布尔(带有大写字母B)是一个Boolean对象,如果不指定值,将默认为null。布尔(以小写'B')是一个布尔原始的,如果不指定值,将默认为false。
Boolean (with a uppercase 'B') is a Boolean object, which if not assigned a value, will default to null. boolean (with a lowercase 'b') is a boolean primitive, which if not assigned a value, will default to false.
Boolean objectBoolean;
boolean primitiveBoolean;
System.out.println(objectBoolean); // will print 'null'
System.out.println(primitiveBoolean); // will print 'false'
祝你好运!
这篇关于为什么对布尔Java的默认值设置为true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!