What's the difference between boolean and Boolean in Java? [duplicate] What's the difference between boolean and Boolean in Java? [duplicate] java java

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


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.


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


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.