Java false 和 Boolean.FALSE 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20090306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between false and Boolean.FALSE?
提问by Ahmad Dwaik 'Warlock'
In C++ windows.h FALSE
is defined as integer which makes sense for some special logic cases, but in Java java.lang.Boolean.FALSE
is defined as boolean and assigned to falsepublic static final Boolean FALSE
and I've seen some people use it.
在 C++windows.h FALSE
中定义为整数,这对某些特殊逻辑情况有意义,但在 Javajava.lang.Boolean.FALSE
中定义为布尔值并分配给 false public static final Boolean FALSE
,我见过一些人使用它。
My question: is there a performance difference between false
and Boolean.FALSE
? in general why do people go and Boolean.FALSE
?
我的问题:false
和之间有性能差异Boolean.FALSE
吗?一般来说,人们为什么去和Boolean.FALSE
?
采纳答案by Jason C
See http://docs.oracle.com/javase/7/docs/api/java/lang/Boolean.html.
请参阅http://docs.oracle.com/javase/7/docs/api/java/lang/Boolean.html。
Boolean.TRUE
and Boolean.FALSE
are not boolean
, they are Boolean
. They are static instances of the two Boolean
wrapper objects that correspond to the boolean
values true
and false
.
Boolean.TRUE
而Boolean.FALSE
不是boolean
,他们是Boolean
。它们是Boolean
对应于boolean
值true
和的两个包装器对象的静态实例false
。
Boolean
is similar to an enum
. The TRUE
and FALSE
instances are the instances returned by Boolean.valueOf()
.
Boolean
类似于enum
. 在TRUE
和FALSE
实例是通过返回的实例Boolean.valueOf()
。
As for performance of primitive vs. wrapper; there is no difference that you would ever need to be concerned about. The TRUE
and FALSE
static instances help performance a bit, and the javadocs recommend using Boolean.valueOf()
as opposed to new Boolean(...)
for that reason. The true
and false
boolean values are a little "lower level", but if you are storing them in a Boolean
(as opposed to boolean
) anyways it's irrelevant.
至于原始与包装器的性能;没有什么区别是您需要担心的。在TRUE
与FALSE
静态实例有助于提高性能一点,的javadoc推荐使用Boolean.valueOf()
,而不是new Boolean(...)
因为这个原因。在true
与false
布尔值是有点“低级别”,但如果你是在它们存储Boolean
(而不是boolean
),反正这是无关紧要的。
You should use whichever makes the most sense for your code and leads to the best readability (and definitely don't start going down the path of thinking of microoptimizations like primitive vs. wrapper types). If you are using a Boolean
, use the object values. If you are using a boolean
, use the primitive values. If you are deciding between Boolean
vs boolean
, use whatever is more appropriate (e.g. a Boolean
can be null
, which may be useful, and also you can't use primitive types for generic type parameters; on the other hand, a boolean
can neverbe null
which could be equally useful).
您应该使用对您的代码最有意义的任何一种,并导致最佳的可读性(绝对不要开始考虑微优化,如原始类型与包装类型)。如果您使用的是Boolean
,请使用对象值。如果您使用的是boolean
,请使用原始值。如果您决定之间Boolean
VS boolean
,使用什么是更合适的(例如,Boolean
可null
,这可能是有用的,你也不能使用原始类型的泛型类型参数;而另一方面,一个boolean
可以永远是null
这可能是同样有用)。
Also note that auto boxing converts the primitive types to one of those two static Boolean
instances, e.g.:
另请注意,自动装箱将原始类型转换为这两个静态Boolean
实例之一,例如:
Boolean a = true;
assert(a == Boolean.TRUE);
As an aside, since you mentioned it: FALSE
is defined in windows.h
for two reasons: 1) Because windows.h
has been in use since C-only days, and C does not have a native bool
type, and 2) it is traditional Microsoft practice to define data types and values with known, explicit sizes and values, esp. for passing data to Windows API functions across DLL boundaries (beyond the scope of this question) and for integrating with other languages that have different representations of "true" and "false". It is entirely unrelated to the reasons for Boolean.FALSE
in Java.
顺便说一句,既然你提到了它:FALSE
定义windows.h
有两个原因:1)因为windows.h
从仅使用 C 的日子开始就一直在使用,并且 C 没有本机bool
类型,以及 2)定义数据类型是 Microsoft 的传统做法和具有已知的、明确的大小和值的值,尤其是。用于跨 DLL 边界(超出本问题的范围)将数据传递给 Windows API 函数,以及与具有不同“真”和“假”表示的其他语言集成。这与Boolean.FALSE
Java中的原因完全无关。
回答by GGrec
Boolean
comes in handy when you need a tri-state variable.
Boolean
当你需要一个三态变量时就派上用场了。
Also, you might want to check out this autoboxing and unboxing tutorial, as well as the rules for how it works.
回答by pappu_kutty
Boolean
is wrapper class for boolean primitive type, same as we have Integer for int.
Boolean
是布尔原始类型的包装类,与我们对 int 的 Integer 相同。
Boolean has many methods to play around primitive type boolean
Boolean 有很多方法来处理原始类型 boolean
http://docs.oracle.com/javase/6/docs/api/java/lang/Boolean.html
http://docs.oracle.com/javase/6/docs/api/java/lang/Boolean.html
回答by Andremoniy
It is a very strange question, because false
is the value of primitive type boolean
, while Boolean.FALSE
is a variable of reference type Boolean
. Its value is reference to the object of type Boolean
which internal boolean state is false
.
这是一个很奇怪的问题,因为false
是原始类型的值boolean
,而Boolean.FALSE
是引用类型的变量Boolean
。它的值是对Boolean
内部布尔状态为的类型对象的引用false
。
回答by Bohemian
false
is a primitive and Boolean.FALSE
is an object, so they're not really comparable.
false
是一个原始Boolean.FALSE
对象并且是一个对象,因此它们实际上没有可比性。
If you assign false
to a Boolean
variable, like this:
如果你分配false
给一个Boolean
变量,像这样:
Boolean b = false;
Java's auto boxing occurs to convert the primitive into an object, so the false
value is lost and you end up with Boolean.FALSE
anyway.
Java 的自动装箱发生将原语转换为对象,因此该false
值丢失了,Boolean.FALSE
无论如何您最终都会得到。
In terms of performance, using a primitive variable would slightlyout-perform using a wrapper Boolean
object, but your choice should be based on readability and basic design decisions rather than on "performance".
在性能方面,使用原始变量会稍微Boolean
优于使用包装器对象,但您的选择应该基于可读性和基本设计决策,而不是“性能”。
回答by Alex Vulaj
In regards to performance, Boolean.FALSE
will return a Boolean
object, which might give you more flexibility to work with.
关于性能,Boolean.FALSE
将返回一个Boolean
对象,这可能会给你更多的工作灵活性。
The primitive alternative takes up less memory
原始替代方案占用更少的内存
回答by Andrei Nicusan
First of all, if you are unclear with this, you need to know that in Java numericals cannot be implicitly cast to booleans, e.g. you cannot compile something like:
首先,如果您对此不清楚,您需要知道在 Java 中数字不能隐式转换为布尔值,例如,您不能编译如下内容:
int a = 1;
if(a){
//something, something
}
Second, some considerations about performance.
There's always a tradeoff.
When using primitive type boolean
, performance should be better because you're using a valuedirectly, which is either true
or false
(obviously, byte-encoded).
When using object type Boolean
, a variable/field will hold a value, but that value cannot be immediately used, as that value is in fact the reference to an object, so you might have a performance penalty. Of course, this penalty is not significant in most cases. And, if you develop a time-critical application, most likely you are not using Java :). Nevertheless, using object types might bring some benefits in development process and safety in use cases implementation (e.g. it allows nulls, which is critical when you map JPA entities to relational tables and there are bit-typed columns that allow nulls; and this is only one scenario where object type is better).
其次,关于性能的一些考虑。总有一个权衡。
使用原始类型时boolean
,性能应该会更好,因为您直接使用值,即true
or false
(显然,字节编码)。
使用对象类型时Boolean
,变量/字段将保存一个值,但该值不能立即使用,因为该值实际上是对对象的引用,因此您可能会受到性能损失。当然,这种惩罚在大多数情况下并不重要。而且,如果您开发时间关键的应用程序,很可能您没有使用 Java :)。然而,使用对象类型可能会给开发过程和用例实现带来一些好处(例如,它允许空值,这在您将 JPA 实体映射到关系表并且存在允许空值的位类型列时至关重要;这只是对象类型更好的一种情况)。
In the end, be aware of boxing/un-boxing, which allows developers to use boolean
-typed variables almost everywhere a Boolean
-typed variable is expected (and the other way around).
最后,请注意装箱/取消装箱,它允许开发人员boolean
几乎在任何需要Boolean
-typed 变量的地方使用-typed 变量(反之亦然)。
回答by deathangel908
Boolean a = null;
if (a) {
// NullPointerException
}
if (a != false) {
// NullPointerException
}
// Caution, comparing by reference, meaning 2 Booleans with same value could be different
if (a != Boolean.FALSE) {
System.out.println("But this one works");
}