Community
Participate
Working Groups
Build Identifier: 20090920-1017 Constructing a new instance of Boolean using 'new Boolean(boolean)' should generate a warning and 'Boolean.valueOf(boolean)' should be suggested. The warning should be disabled with a comment such as //$NEW-BOOLEAN$ or with an annotation such as @SuppressWarnings{"new-boolean"}. Constructing new Booleans are almost always a bad idea; their values can only be compared with .equals(Object) and not with ==. Furthermore it takes more (not really that much) memory while they can not be changed (all boxing classes are immutable). Reproducible: Always
(In reply to comment #0) > warning should be disabled with a comment such as //$NEW-BOOLEAN$ or with an > annotation such as @SuppressWarnings{"new-boolean"}. Using SuppressWarnings is possible, not the other one. I don't think this would deserve such a special comment. > Constructing new Booleans are almost always a bad idea; their values When this would be a good idea? I don't believe this very special case deserves a specific warning. Markus, what do you think ?
I also wouldn't add a warning for that special case. new Boolean(..) is just one instance of the general "use the factory instead of calling the constructor" family of problems. These problems are not general problems that often occur in the Java language, but problems that depend on specific libraries. These kinds of problems don't fit well into our current "Potential programming problems" category. They'd better be implemented in a third=party plug-in for now.
Closing as WONTFIX. It is easy using a search request to find all usage of this constructor. It doesn't deserve a specific treatment from the compiler.
Verified for 3.6M7.