Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a valid reason to code a false boolean as "Boolean.FALSE" in java?

Tags:

java

So code reviewers are complaining about code like this:

boolean myFlag = false; 

They are saying it should be:

boolean myFlag = Boolean.FALSE; 

Is this just some fetish with not using keywords or is there a valid reason to do this?

like image 525
Rocky Pulley Avatar asked Jul 19 '11 13:07

Rocky Pulley


2 Answers

No, that's completely pointless. It would make sense to use:

// Note capital B Boolean myFlag = Boolean.FALSE; 

to avoid the call to Boolean.valueOf (autoboxing) but in your code there is no boxing, and their suggestion introduces an unnecessary unboxing operation.

As ever though, if someone suggests something and you don't understand why, your first port of call should be asking them.

like image 78
Jon Skeet Avatar answered Sep 18 '22 05:09

Jon Skeet


There's nothing wrong with using keyword false. In fact, in your code you'd be silly to use Boolean.False since there is an implicit auto unboxing that has to occur to assign it to your primitive field/variable (Boolean.False is a Boolean and not a boolean).

like image 26
whaley Avatar answered Sep 19 '22 05:09

whaley