Java 我什么时候应该使用布尔值而不是布尔值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18953251/
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
When should I use Boolean instead of boolean?
提问by TheLostMind
When should I use Boolean instead of boolean?. I mean, why would I want to have a null value in a variable which should contain either "true" or "false".. One spontaneous answer (of most people) would be if the value is unknown. i.e, if we don't know whether the value is true or false. But from a programming perspective, I think using Boolean may break the code as we will not know whats inside. So, I think using the primitive type is better than the wrapper.. Correct me if I am wrong.
我什么时候应该使用布尔值而不是布尔值?。我的意思是,为什么我要在一个应该包含“真”或“假”的变量中拥有一个空值......一个自发的答案(大多数人)是如果该值未知。即,如果我们不知道该值是真还是假。但是从编程的角度来看,我认为使用 Boolean 可能会破坏代码,因为我们不知道里面是什么。所以,我认为使用原始类型比使用包装器更好。如果我错了,请纠正我。
采纳答案by chrylis -cautiouslyoptimistic-
Generally speaking, the wrapper classes are used in cases where an object is required or strongly preferred. Outside of these situations, it's better to use the primitive types, since they have lower overhead, you can use ==
, etc. There are two and a half major situations where this is frequently seen:
一般来说,包装类用于需要或强烈首选对象的情况。在这些情况之外,最好使用原始类型,因为它们的开销较低,您可以使用==
等。有两种半主要情况经常看到这种情况:
- Collections. This is now a subset of the next case, but even before Java 5 the Collections classes only supported objects as keys and values, and this hasn't changed.
- Generics. Generic types can only work with objects, not primitives, and so if you're using "boolean" as a type parameter, it has to be the wrapper class. For example, if you're using a
Future
, you have to use aBoolean
instead of aboolean
. (HT @user949300) - ORM. JPA and other ORM systems technically can use primitive fields, but it's customary to use the wrapper classes, since the overhead is high enough that that doesn't really matter anyway, and the wrapper classes can represent a
NULL
value that might be present in the database. It's usually still better to forbid nulls and use a primitive for booleans, though, since semantically a default is usually better than "undefined".
- 收藏。现在这是下一个案例的一个子集,但即使在 Java 5 之前,Collections 类也只支持对象作为键和值,并且这一点没有改变。
- 泛型。泛型类型只能用于对象,不能用于基元,因此如果您使用“boolean”作为类型参数,它必须是包装类。例如,如果您使用的是 a
Future
,则必须使用 aBoolean
而不是 aboolean
。(HT@user949300) - ORM。JPA 和其他 ORM 系统在技术上可以使用原始字段,但习惯上使用包装类,因为开销足够高,无论如何都无关紧要,包装类可以表示
NULL
可能存在于数据库中的值. 不过,通常最好还是禁止空值并对布尔值使用原语,因为从语义上讲,默认值通常比“未定义”要好。
Since boolean values are restricted to either true
or false
, it's uncommon to see them used in Collections or Generics; generally speaking, if you'd have a boolean as a value, you'll just use Collection#contains
instead.
由于布尔值仅限于true
或false
,因此在集合或泛型中很少看到它们;一般来说,如果你有一个布尔值作为值,你只需使用Collection#contains
。
回答by Lokesh
What if you need to use collections? Collection will not store primitive types, you need to store object there. Collections provide so many utility apis, so if you want to use them them a Boolean object is need as collection will need objects. Although you can always use autoboxing which means you are shielded from object creation and collection takes care of it internally.
如果需要使用集合怎么办?集合不会存储原始类型,您需要在那里存储对象。集合提供了如此多的实用 api,因此如果您想使用它们,则需要一个布尔对象,因为集合将需要对象。尽管您始终可以使用自动装箱,这意味着您可以避免对象创建,并且集合会在内部处理它。
回答by Suresh Atta
IMHOthe primitive is better.
恕我直言,原始人更好。
Always favor primitives over wrappers. Wherever I am able to use primitives, I go for them because at run time, if we use wrappers, boxing conversionsand unboxing conversionshappen, and obviously that takes more time. If you use the primitive there, you save that time.
总是喜欢原语而不是包装器。只要我能够使用原语,我去为他们,因为在运行时,如果我们使用包装,装箱转换和拆箱转换发生,显然这需要更多的时间。如果您在那里使用原语,则可以节省时间。
And as usual it depends on your requirements whether you need a Object
(which can be null
) or you can use a primitive (which cannot be null
) in your situation.
和往常一样,这取决于您的要求,在您的情况下是需要一个Object
(可以是null
)还是可以使用原语(不能是null
)。
For example: Assume you are dealing with collection's then you have no option's,You have to use wrappers :).
例如:假设您正在处理集合,那么您别无选择,您必须使用包装器 :)。
回答by Raghavendra Reddy Busireddy
The Wrapper classes will be in cases where an object is required or strongly preferred like storing the objects in Collections, cache or session etc, where it requires an object (if not, JRE will convert the primitives to Wrapper classes before they are stored in secondary cache). The below link will explain in better:
Wrapper 类将用于需要或强烈首选对象的情况,例如将对象存储在 Collections、缓存或会话等中,它需要一个对象(如果不是,JRE 将在将原语存储在辅助类之前将它们转换为 Wrapper 类)缓存)。下面的链接将更好地解释:
回答by AppX
Boolean have 3 possible values (null, true, false) while boolean can only be (true, false).
Boolean 有 3 个可能的值 (null, true, false) 而 boolean 只能是 (true, false)。
回答by Idan Arye
Boolean
is an object, so you can use it with generics. For example, you can have Map<String,Boolean>
to store a true\false value for each string(=key). You can't do Map<String,boolean>
, because boolean
is not an object - specifically, it's not a subclass of Object
. Generics are compile-time wrappers, so Map<Foo,Bar>
is actually Map<Object,Object>
with smart casting, no matter what Foo
and Bar
are - as long as they are classes or array types. But they can't be primitives.
Boolean
是一个对象,因此您可以将它与泛型一起使用。例如,您可能必须Map<String,Boolean>
为每个字符串 (=key) 存储一个 true\false 值。你不能这样做Map<String,boolean>
,因为boolean
它不是一个对象——具体来说,它不是Object
. 泛型是编译时的外包装,所以Map<Foo,Bar>
实际上是Map<Object,Object>
与智能铸件,不管是什么Foo
以及Bar
是-只要它们是类或数组类型。但它们不能是原语。
回答by user949300
I greatly prefer primitives. However, Booleans are necessary:
我非常喜欢原语。但是,布尔值是必要的:
- When they go into Collections
- When you need to allow for a null value. In my experience, this is mainly if they are stored in a database and you need null to indicate that they haven't been read yet, or the user hasn't filled in some form yet.
- 当他们进入 Collections 时
- 当您需要允许空值时。根据我的经验,这主要是如果它们存储在数据库中并且您需要 null 来指示它们尚未被读取,或者用户尚未填写某种表格。
回答by VikramV
Booolean is an object/reference type that wraps a boolean whereas boolean in a primitive type.
Booolean 是一种对象/引用类型,它包装一个布尔值,而布尔值则是一个原始类型。
Boolean- You would get more methods which will be useful.
Boolean- 你会得到更多有用的方法。
boolean- Will save you lot of memory. But if you use Boolean.valueOf(value) of new Boolean(value)
, that shouldn't be a cause.
boolean- 将为您节省大量内存。但是如果你使用Boolean.valueOf(value) of new Boolean(value)
,那不应该是一个原因。
Converting between primitives and objects like this is known as boxing/unboxing.
像这样在基元和对象之间进行转换称为装箱/拆箱。
Click on the below links for more info:
单击以下链接了解更多信息:
http://javaeye.wordpress.com/2008/06/17/boxing-and-unboxing-conversion/
http://javaeye.wordpress.com/2008/06/17/boxing-and-unboxing-conversion/
http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html