boolean
is a primitive type, that is, it is a number that occupies 1 byte (although it is not specified that it has this size) and is considered a type by value, that is, its value is its own identity, it exists in itself. The comparison of values is direct.
Its default value is false
(that would be a 0).
It can be converted implicitly to a text ("false" or "true") or a number (0 or 1) can be used to represent false or true in type, where necessary.
Boolean
is a class encapsulating a boolean
and is a type by reference, so its actual value is a pointer that points to an object whose value is the boolean. Obviously the class derives Object
and has all the characteristics of an object like any other.
It is a way of passing the data by reference in parameters methods or use where you need an indirect.
The default value of it null
. It has 3 possible states and the fact that it needs the state null
is a reason to use it.
The comparison is by the method equals()
, if using the operator ==
will compare the pointers of objects even having the same value. Unless you use booleanValue()
that takes the primitive within the object, but there the comparison is not with the object but with primitives extracted from the classes.
The compiler does a trick to the Boxing seem transparent, but he’s creating a new instance in the heap.
Boolean x = true; //na verdade é traduzido para o abaixo
Boolean x = Boolean.valueOf(true);
Imagine that in addition to the space occupied by the pointer (4 or 8 bytes) it still stores an object that can occupy 20 bytes or more depending on the architecture. All this because of a single bit.
The indirect as well as having a higher cost of allocation and copy generates more miss cache in the access that needs to occur in two steps: access the value (pointer address) and then the value of the object.
The preference is always to use the primitive type, is faster and occupies less memory. Given the inefficiency the class option only if it is really necessary to have a reference (in other languages it is possible to have a reference without creating this whole problem, but not in Java). An example of need in the current version is the use with Generics (this will possibly change in Java 15 or higher).
ArrayList<Boolean> = lista = new ArrayList<>(); //válido
ArrayList<boolean> = lista = new ArrayList<>(); //inválido
See about it in What is the difference between "Generics" (Java/C#) and "template" (C++) and What are the differences between Generic Types in C# and Java?.
With methods:
void metodo(boolean x) {x = false;} //quando terminar o método, não muda nada no argumento
void metodo(Boolean x) {x = false;} //quando terminar o método, o argumento valerá false
I put in the Github for future reference.
Rumors :) It is ideal to use primitive types in Java?
For comparison effect C# achieves all 3 main effects reported with the primitive type: use as generic type, can be explicitly passed by reference without doing Boxing and has the cancellable option bool?
which is still a type per value occupying only 2 bytes in total (could be 1 if there was optimization).
I know that
Boolean
can receivenull
whileboolean
no, and you can just useBoolean
usGenerics
, for exampleConvert<Boolean>()
...– Marco Giovanni
Similar question and similar answer : int and Integer - Java
– Guilherme Lautert
I often say that
Boolean
without proper mental preparation is a request for unexpected and unexplained NPE– Jefferson Quesado