What is the difference between Boolean and Boolean?

Asked

Viewed 11,436 times

29

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!");

The Boolean (capital B) being a class, in this case it did NOT need to be instantiated before using it. There is some difference between Boolean and boolean? What can influence my application at the time of the declaration of the same?

  • 2

    I know that Boolean can receive null while boolean no, and you can just use Boolean us Generics, for example Convert<Boolean>()...

  • 4

    Similar question and similar answer : int and Integer - Java

  • I often say that Boolean without proper mental preparation is a request for unexpected and unexplained NPE

2 answers

36


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

23

  • Boolean capital letter at the beginning is a class, it is the same as java.lang.Boolean

  • boolean 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 to string (or do a parse)

Methods of the java.lang.Boolean class

  • 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 not null and is a boolean object representing the same boolean value as this object.

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

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

  • static boolean parseBoolean(String s) parses the string for boolean.

  • String toString() returns to String representing the object.

  • static String toString(boolean b) returns to String representing the value of the argument.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.