Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialization of a boolean with a Boolean.FALSE/.TRUE - why?

Tags:

java

boolean

In some of our companys projects code I often read something like this:

boolean foo = Boolean.FALSE;

Besides the fact that AFAIK I only have to initialize local variables in Java at all (no random values like in Pascal) and besides the fact that especially for booleans I often WANT to have an initialization, what do I miss here? Why not:

boolean foo = false;

I don't get it. And code analyzation tools like PMD and Findbugs mark it, too. But why?

Edit: Without really knowing much about the bytecode except that it is there I created an example class and decompiled it. The Boolean.FALSE went to:

0: getstatic #15 // Field java/lang/Boolean.FALSE:Ljava/lang/Boolean; 
3: invokevirtual #21 // Method java/lang/Boolean.booleanValue:()Z 
6: istore_1 

The 'false' variant went to:

0: iconst_1 
1: istore_1 

So without knowing too much about this, I'd guess that more statements means more time to execute so it's not only wrong but also slower in the long run.

like image 301
Campfire Avatar asked Oct 18 '12 12:10

Campfire


People also ask

Does a boolean initialize as true or false?

The default value of Boolean is False .

Why is boolean false true?

A Boolean variable has only two possible values: true or false. It is common to use Booleans with control statements to determine the flow of a program. In this example, when the boolean value "x" is true, vertical black lines are drawn and when the boolean value "x" is false, horizontal gray lines are drawn.

Are boolean arrays initialized to false?

The boolean array can be used to store boolean datatype values only and the default value of the boolean array is false. An array of booleans are initialized to false and arrays of reference types are initialized to null. In some cases, we need to initialize all values of the boolean array with true or false.

What happens if a boolean statement is false?

Notice that the boolean in the if-test (true or false) happens to be the same as the value we want to return. If the test value is true, we return true. If the test value is false, we return false. So just return the test value directly!


1 Answers

boolean foo = Boolean.FALSE;

This is strange and unnecessarily complicated code, written by someone who likely didn't know Java very well. You shouldn't write code like this, and PMD and FindBugs are right to mark this.

Boolean.FALSE is a java.lang.Boolean object that gets auto-unboxed; the compiler essentially translates this to:

boolean foo = Boolean.FALSE.booleanValue();

I don't have to initialize variables in Java at all...

Member variables do not need to be initialized explicitly; if you don't, they'll be initialized with a default value (which is false in the case of boolean). Local variables do need to be explicitly initialized; if you try to use a local variable without initializing it, the compiler will give you an error.

like image 102
Jesper Avatar answered Sep 27 '22 21:09

Jesper