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

JavaGwtBoolean

Java Problem Overview


I would like to understand the difference between the Boolean and boolean types 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 Solutions


Solution 1 - Java

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

  • boolean can be yes or no
  • Boolean can be yes, no or 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.

Solution 2 - Java

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

boolean is a java primitive type whereas Boolean is an object/reference type that wraps a 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/

Why box you ask?

http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html

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

Solution 3 - Java

In Java, a boolean is a literal true or false, while Boolean is an object wrapper for a boolean.

There is seldom a reason to use a Boolean over a boolean except in cases when an object reference is required, such as in a List.

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

Solution 4 - Java

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

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

Solution 5 - Java

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.

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").

Hope that helps.

Solution 6 - Java

As far as GWT, they are the same in GWT as there are in java.
boolean is a primative and Boolean in an object wrapper.

Solution 7 - Java

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()

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.

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).

Solution 8 - Java

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.

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.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionroundcrisisView Question on Stackoverflow
Solution 1 - JavarustyshelfView Answer on Stackoverflow
Solution 2 - JavacakeforcerberusView Answer on Stackoverflow
Solution 3 - JavaMichael MyersView Answer on Stackoverflow
Solution 4 - JavaMichael MunseyView Answer on Stackoverflow
Solution 5 - JavaAllain LalondeView Answer on Stackoverflow
Solution 6 - JavaNickView Answer on Stackoverflow
Solution 7 - JavaDavid NoulsView Answer on Stackoverflow
Solution 8 - JavaMaheshbabu JammulaView Answer on Stackoverflow