What difference between Boolean and boolean?

I performed some tests using Boolean and boolean and apparently returned the same result. See below:

Boolean bool  = true;
boolean bool2 = true;

if(bool)
    Log.wtf(TAG, "Funciona!");    

if(bool2)
    Log.wtf(TAG, "Funciona também!");

O Boolean (with B uppercase) being a class, in this case above it did not need to be instantiated before using it. Is there any difference between Boolean and boolean? What can influence my application at the time of its declaration?

Author: UzumakiArtanis, 2016-10-24

2 answers

boolean it 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 straightforward.

Its default value is false (which would be a 0).

It can be implicitly converted to a text ("false" or "true") or a number (0 or 1) can be used to represent false or true in the like, where it's needed.

Boolean it is a class that encapsulates a boolean and is a type by reference, so its value in fact is a pointer that points to an object whose value is the boolean. Obviously the class derives Object and has all characteristics of an object like any other.

Is a way to pass the data by reference in parameters of methods or use where it needs an indirection.

Its default value null. It has 3 states possible and the fact that you need the null state is a reason to use it.

The comparison is done by the method equals(), if you use the operator == it will compare the pointers of the objects, even if they have the same value. Unless you use booleanValue() that takes the primitive inside the object, but there the comparison is not with the object but with primitives extracted from the classes.

The compiler does a trick to make boxing look transparent, but it is creating a new one instance in 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 for the sake of a single bit.

Indirection in addition to having a higher allocation and copy cost generates more cache miss in access that needs to occur in two steps: access the value (pointer address) and then the value of the object.

The preference is always use the primitive type, it is faster and takes up less memory. Given the inefficiency the option for the class 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 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 this 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

Rumors:) is it ideal to use primitive types in Java?

For comparison purposes C # achieves all 3 main effects reported with the primitive type: use as a generic type, can be passed explicitly by reference without doing boxing and has the nullable option bool? which is still a type by value occupying only 2 bytes in total (could be 1 if there was optimization).

 34
Author: Maniero, 2020-11-12 10:58:58
  • Boolean with capital letter in the beginning is a class, it is the same as java.lang.Boolean

  • boolean it is a primitive type of comparison of two values, true or false.

You can use both for the same purpose, the difference is that the class has methods to convert to string for example, or convert from string (or make a parse)

Java class methods.lang.Boolean

  • bool booleanValue() the return value of this method is a primitive boolean.

  • static int compare(boolean x, boolean y) compares two boolean values.

  • int compareTo(Boolean b) compares the value of the current class with another class.

  • boolean equals(Object obj) returns true if and only if the argument is non-null and is a Boolean object that represents the same boolean value as this object.

  • static boolean getBoolean(String name) returns true if and only if the property of the system named by the argument exists and is equal to the string "true".

  • int hashCode() returns a hash for the current object.

  • static boolean parseBoolean(String s) parse the string to boolean.

  • String toString() returns String that represents the object.

  • static String toString(boolean b) returns String which represents the value of the argument.

  • static Boolean valueOf(boolean b) returns the instance java.lang.Boolean that represents the specific boolean value.

  • static Boolean valueOf(String s) returns the java.lang.Boolean represented by the String in the argument

 22
Author: Guilherme Nascimento, 2018-08-30 12:45:20