Thursday, 10 December 2015

true vs. Boolean.TRUE

I saw the following code below at my work, and it got me thoroughly confused. I found the statement above to be unnecessarily complicated.

private boolean useAsReferenceModel = Boolean.FALSE;

Boolean.FALSE returns a Boolean, which is a wrapper2 around the boolean primitive types.

So, in the example above, the Boolean wrapper is auto-unboxed3 to the primitive type false.

So, I changed the code to the simple and straightforward:
private boolean useAsReferenceModel = false;
Let us not make the world more complicated than it really is.

References

[1] StackOverflow - Java what is the difference between false and Boolean.FALSE
http://stackoverflow.com/questions/20090306/java-what-is-the-difference-between-false-and-boolean-false
[2] Wikipedia - Primitive Wrapper Classes
https://en.wikipedia.org/wiki/Primitive_wrapper_class
[3] Oracle JavaDoc - Autoboxing and unboxing
https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

No comments:

Post a Comment