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.

- 156,901
- 35
- 231
- 235

- 17,276
- 14
- 60
- 92
8 Answers
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.

- 44,963
- 37
- 98
- 104
-
1I am hoping that Boolean can be passed to a method and the Boolean can be set and it will be known by the caller ... and vice versa. – Brian Reinhold Mar 29 '17 at 19:45
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

- 4,657
- 6
- 32
- 42
-
+1 for the box/unbox mention :-). Not being able to put primitive, like `int` or `boolean`, in collections are, if I understand it well, the main (if not the only reason) for the wrapper Objects like `Boolean`, `Integer` and the like... – Julien Mar 08 '21 at 15:23
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.

- 188,989
- 46
- 291
- 292
Because Boolean can be null, it can be used for lazy loading.
if(hasRoots == null){
calculateRoots();
}

- 3,740
- 1
- 25
- 15
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.

- 91,574
- 70
- 187
- 238
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).

- 1,895
- 12
- 21
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.

- 357
- 1
- 2
- 11