Java 中的 boolean 和 Boolean 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1295170/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:54:58  来源:igfitidea点击:

What's the difference between boolean and Boolean in Java?

javagwtboolean

提问by roundcrisis

I would like to understand the difference between the Booleanand booleantypes in Java, specifically as they relate to GWT. I know that methods are not supported but I want more info if it is available.

我想了解Java 中的Booleanboolean类型之间的区别,特别是因为它们与 GWT 相关。我知道不支持这些方法,但如果可用,我想要更多信息。

采纳答案by cakeforcerberus

I'm not sure if the GWT factor makes a difference but in general:

我不确定 GWT 因素是否有所作为,但总的来说:

booleanis a java primitive type whereas Booleanis an object/reference type that wraps a boolean

boolean是一个 java 原始类型,而Boolean是一个对象/引用类型,它包装了一个布尔值

Converting between primitives and objects like this is known as boxing/unboxing.

像这样在基元和对象之间进行转换称为装箱/拆箱。

Here is more info:

这是更多信息:

http://javaeye.wordpress.com/2008/06/17/boxing-and-unboxing-conversion/

http://javaeye.wordpress.com/2008/06/17/boxing-and-unboxing-conversion/

Why box you ask?

你问为什么要盒子?

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

http://www.javapractices.com/topic/TopicAction.do?Id=197

http://www.javapractices.com/topic/TopicAction.do?Id=197

回答by Michael Myers

In Java, a booleanis a literal trueor false, while Booleanis an object wrapper for a boolean.

在 Java 中, aboolean是一个字面量trueor false,而 whileBoolean是 a 的对象包装器boolean

There is seldom a reason to use a Booleanover a booleanexcept in cases when an object reference is required, such as in a List.

很少有理由在 aBoolean上使用 a ,boolean除非需要对象引用的情况,例如在 a 中List

Booleanalso contains the static method parseBoolean(String s), which you may be aware of already.

Boolean还包含parseBoolean(String s)您可能已经知道的静态方法。

回答by Allain Lalonde

Java has primitive types (int, boolean, float, etc) and anytime you wish to use them as an instance of an object they are wrapped in an associated Class type. For example, booleans get wrapped by Booleans, int as Integer etc.

Java 具有原始类型(int、boolean、float 等),并且只要您希望将它们用作对象的实例,它们就会被包装在关联的 Class 类型中。例如,布尔值被布尔值包裹,int 为整数等。

It has its benefits too. boolean has no helper methods (since it's not a class), but Boolean does. So, if you wanted to convert a string to a boolean you could try Boolean.valueOf("true").

它也有它的好处。boolean 没有辅助方法(因为它不是一个类),但 Boolean 有。因此,如果您想将字符串转换为布尔值,您可以尝试 Boolean.valueOf("true")。

Hope that helps.

希望有帮助。

回答by Nick

As far as GWT, they are the same in GWT as there are in java.
booleanis a primative and Booleanin an object wrapper.

就 GWT 而言,它们在 GWT 中与在 java 中相同。
boolean是对象包装器中的原始和布尔值

回答by rustyshelf

It's fairly Simple and the same for GWT and Java:

对于 GWT 和 Java,它相当简单且相同:

  • boolean can be yes or no
  • Boolean can be yes, no or NULL.
  • boolean 可以是 yes 或 no
  • 布尔值可以是 yes、no 或 NULL。

So unless you need the NULL (like for example your loading the field from the database, and you want NULL to be different to false) then stick to boolean.

因此,除非您需要 NULL(例如您从数据库加载字段,并且您希望 NULL 与 false 不同),否则请坚持使用布尔值。

回答by David Nouls

According to the GWT JRE emulation docs (http://code.google.com/webtoolkit/doc/1.6/RefJreEmulation.html) these methods are supported on the Boolean type: Boolean(boolean), Boolean(String), parseBoolean(String), toString(boolean), valueOf(boolean), valueOf(String), booleanValue(), compareTo(Boolean), equals(Object), hashCode(), toString()

根据 GWT JRE 仿真文档 ( http://code.google.com/webtoolkit/doc/1.6/RefJreEmulation.html),布尔类型支持这些方法:Boolean(boolean), Boolean(String), parseBoolean(String) )、toString(boolean)、valueOf(boolean)、valueOf(String)、booleanValue()、compareTo(Boolean)、equals(Object)、hashCode()、toString()

as to the difference between boolean and Boolean object types. Boolean objects can be in 3 states, so it is not exactly the same. But if that makes a difference in GWT (performance wise) I don't have a clue, my guess is that it does not matter much since the GWT compiler will optimize the code and most operations could simply map to native javascript boolean operations.

关于 boolean 和 Boolean 对象类型之间的区别。布尔对象可以有 3 种状态,因此并不完全相同。但是,如果这对 GWT(性能方面)产生影响,我不知道,我的猜测是这并不重要,因为 GWT 编译器会优化代码,并且大多数操作可以简单地映射到本机 javascript 布尔操作。

But as usual: to be certain you must measure (and take into account that this might differ based on what browser/version you are measuring).

但像往常一样:为了确定您必须进行测量(并考虑到这可能会因您测量的浏览器/版本而异)。

The Boolean object type is normally not used very often since the boolean native type is more natural (you don't need to check for null all the time).

Boolean 对象类型通常不经常使用,因为 boolean 原生类型更自然(您不需要一直检查 null)。

回答by Michael Munsey

Because Boolean can be null, it can be used for lazy loading.

因为 Boolean 可以为 null,所以可以用于延迟加载。

if(hasRoots == null){
   calculateRoots();
}

回答by Maheshbabu Jammula

boolean is a primitive type whereas Boolean is wrapper class.Same applies for (int,Integer),(long,Long) etc. Wrapper classes "wrap" the respective primitive data type into an object of that class.

boolean 是一种原始类型,而 Boolean 是包装类。同样适用于 (int,Integer),(long,Long) 等。包装类将相应的原始数据类型“包装”到该类的对象中。

They are used with collections, as primitive types are not allowed with collections.Also using wrapper classes give you access to many methods that you can call on that object.For eg. Character wrapper class have methods like:

它们与集合一起使用,因为集合中不允许使用原始类型。此外,使用包装类可以让您访问可以在该对象上调用的许多方法。例如。字符包装器类具有如下方法:

isDigit() – to determine whether the character is digit. isLower() – to determine whether the character is lower case alphabet. is Letter() – to determine whether the character is an alphabet.

isDigit() – 判断字符是否为数字。isLower() – 确定字符是否为小写字母。is Letter() – 确定字符是否为字母表。

we cannot use the above methods if we use a primitive type as compared to a wrapper class.

如果我们使用原始类型而不是包装类,我们就不能使用上述方法。