- Operating system: Windows 10
- IDE name and flavor/env: Eclipse STS
This simple piece of code gives me a warning:
final Boolean cardEnabled = parseAndGet("card-enabled");
cardButton.setVisible(
cardEnabled == null ? false : cardEnabled.booleanValue()
);
Method parseAndGet() uses a map of properties and returns null, Boolean.TRUE or Boolean.FALSE.
Setter of readCard needs boolean as parameter and that’s why we have to check for null.
In this case ‘false’ is marked with warning ‘Remove the unnecessary boolean literal :: Boolean literals should not be redundant (java:S1125)’.
Of course, I can write this the other way and I will but I don’t get it why I’m getting a warning.
// other way #1 (that I'm going to use)
final Boolean cardEnabled = parseAndGet("card-enabled");
cardButton.setVisible( Boolean.TRUE.equals( cardEnabled ) );
// other way #2
Boolean cardEnabled = parseAndGet("card-enabled");
if (cardEnabled == null) {
cardEnabled = Boolean.FALSE;
}
cardButton.setVisible( cardEnabled.booleanValue() );