Default value of boolean and Boolean in Java

人走茶凉 提交于 2019-11-26 06:19:12

问题


What are the default values of boolean (primitive) and Boolean (primitive wrapper) in Java?


回答1:


The default value for a Boolean (object) is null.
The default value for a boolean (primitive) is false.




回答2:


The default value of any Object, such as Boolean, is null.

The default value for a boolean is false.

Note: Every primitive has a wrapper class. Every wrapper uses a reference which has a default of null. Primitives have different default values:

boolean -> false

byte, char, short, int, float -> 0

float, double -> 0.0

Note (2): void has a wrapper Void which also has a default of null and is it's only possible value (without using hacks).




回答3:


boolean
Can be true or false.
Default value is false.

(Source: Java Primitive Variables)

Boolean
Can be a Boolean object representing true or false, or can be null.
Default value is null.




回答4:


If you need to ask, then you need to explicitly initialize your fields/variables, because if you have to look it up, then chances are someone else needs to do that too.

The value for a primitive boolean is false as can be seen here.

As mentioned by others the value for a Boolean will be null by default.




回答5:


Boolean is an Object. So if it's an instance variable it will be null. If it's declared within a method you will have to initialize it, or there will be a compiler error.

If you declare as a primitive i.e. boolean. The value will be false by default if it's an instance variable (or class variable). If it's declared within a method you will still have to initialize it to either true or false, or there will be a compiler error.




回答6:


An uninitialized Boolean member (actually a reference to an object of type Boolean) will have the default value of null.

An uninitialized boolean (primitive) member will have the default value of false.




回答7:


There is no default for Boolean. Boolean must be constructed with a boolean or a String. If the object is unintialized, it would point to null.

The default value of primitive boolean is false.

http://download.oracle.com/javase/6/docs/api/java/lang/Boolean.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html



来源:https://stackoverflow.com/questions/6226290/default-value-of-boolean-and-boolean-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!