Most awkward/misleading method in Java Base API ?
Posted
by JG
on Stack Overflow
See other posts from Stack Overflow
or by JG
Published on 2009-08-18T05:35:34Z
Indexed on
2010/05/13
17:14 UTC
Read the original article
Hit count: 218
I was recently trying to convert a string literal into a boolean
, when the method "boolean Boolean.getBoolean(String name)
" popped out of the auto-complete window. There was also another method ("boolean Boolean.parseBoolean(String s)
") appearing right after, which lead me to search to find out what were the differences between these two, as they both seemed to do the same.
It turns out that what Boolean.getBoolean(String name)
really does is to check if there exists a System
property (!) of the given name and if its value is true
. I think this is very misleading, as I'm definitely not expecting that a method of Boolean
is actually making a call to System.getProperty
, and just by looking at the method signature, it sure looks (at least to me) like it should be used to parse a String
as a boolean
. Sure, the javadoc states it clearly, but I still think the method has a misleading name and is not in the right place. Other primitive type wrappers, such as Integer
also have a similar method.
Also, it doesn't seem to be a very useful method to belong in the base API, as I think it's not very common to have something like -Darg=true
. Maybe it's a good question for a Java position interview: "What is the output of Boolean.getBoolean("true")
?". I believe a more appropriate place for those methods would be in the System
class, e.g., getPropertyAsBoolean
; but again, I still think it's unnecessary to have these methods in the base API. It'd make sense to have these in something like the Properties
class, where it's very common to do this kind of type conversions.
What do you think of all this ? Also, if there's another "awkward" method that you're aware of, please post it.
N.B. I know I can use Boolean.valueOf
or Boolean.parseBoolean
to convert a string literal into a boolean
, but I'm just looking to discuss the API design.
© Stack Overflow or respective owner