坑爹的 Boolean.getBoolean(String name)
2013-02-25 14:56
148 查看
1、今天遇到这样一件事:想把String类型的true和false转换成boolean的原生类型,于是顺手的用Boolean.<alt+/>(快捷键),jdk提供了一个static 的 getBoolean(name)方法,调用之后并不是我预期的结果。最先值得怀疑的应该是自己代码有问题。细查后原来是被此方法名误解了。
2、这个方法名起的很邪恶。就是被他的字面意思给误解了。
3、仔细看了看文档
jdk 写道
Returns true if and only if the system property named by the argument exists and is equal to the string "true". (Beginning with version 1.0.2 of the JavaTM platform, the test of this string is case insensitive.) A system property is accessible through getProperty,
a method defined by the System class.
当且仅当系统属性的名字存在且他的值为“true”是才返回true,不知道大家在第一次有没有被误导的,我反正被他欺骗了。
4、提供这样的一个方法究竟有什么用处了。我相信作者van Hoff最初的想法是很好的,在没有这个方法之前,如果想获取系统属性的值转换成Boolean类型操作的话通常就是:
Java代码
String value = System.getProperty(key);
boolean b = Boolean.valueOf(value);
//TODO
而现在可以直接用getBoolean方法了,其实他也就是在这方法里封装了一下string——>boolean转换的步骤,请看源代码:
Java代码
public static boolean getBoolean(String name) {
boolean result = false;
try {
result = toBoolean(System.getProperty(name));
} catch (IllegalArgumentException e) {
} catch (NullPointerException e) {
}
return result;
}
5、对此方法的一些见解:本应是作为System类中的一个方法,现在把他放在Boolean类中,试想如果是这样写:
Java代码
System.getPropertyAsBoolean(name)
是不是见名知义了呢?
6、瞎想:作者van Hoff本是好意,没想到弄巧成拙,误导不少初次使用此方法的人,查看了下写System类和Boolean类的作者分别为:unascribed和van Hoff,他们正喝着咖啡,一边聊着天,一边写着代码,聊着聊着······(由大家补充吧),结果。。。
2、这个方法名起的很邪恶。就是被他的字面意思给误解了。
3、仔细看了看文档
jdk 写道
Returns true if and only if the system property named by the argument exists and is equal to the string "true". (Beginning with version 1.0.2 of the JavaTM platform, the test of this string is case insensitive.) A system property is accessible through getProperty,
a method defined by the System class.
当且仅当系统属性的名字存在且他的值为“true”是才返回true,不知道大家在第一次有没有被误导的,我反正被他欺骗了。
4、提供这样的一个方法究竟有什么用处了。我相信作者van Hoff最初的想法是很好的,在没有这个方法之前,如果想获取系统属性的值转换成Boolean类型操作的话通常就是:
Java代码
String value = System.getProperty(key);
boolean b = Boolean.valueOf(value);
//TODO
而现在可以直接用getBoolean方法了,其实他也就是在这方法里封装了一下string——>boolean转换的步骤,请看源代码:
Java代码
public static boolean getBoolean(String name) {
boolean result = false;
try {
result = toBoolean(System.getProperty(name));
} catch (IllegalArgumentException e) {
} catch (NullPointerException e) {
}
return result;
}
5、对此方法的一些见解:本应是作为System类中的一个方法,现在把他放在Boolean类中,试想如果是这样写:
Java代码
System.getPropertyAsBoolean(name)
是不是见名知义了呢?
6、瞎想:作者van Hoff本是好意,没想到弄巧成拙,误导不少初次使用此方法的人,查看了下写System类和Boolean类的作者分别为:unascribed和van Hoff,他们正喝着咖啡,一边聊着天,一边写着代码,聊着聊着······(由大家补充吧),结果。。。
相关文章推荐
- 解决User.Identity.IsAuthenticated==false 或User.Identity.Name==string.empty的问题
- 通过代码的string name 找到到value下对应的string name
- 解决mysql插入数据时出现Incorrect string value: '\xF0\x9F...' for column 'name' at row 1的异常
- Boolean.getBoolean(String name)
- tp报错 General error: 1366 Incorrect string value: '\xE6\xB5\x8B\xE8\xAF\x95' for column 'name' at row
- 用java String类的getBytes(String charsetName)和String(byte[] bytes, String charsetName)解决乱码问题
- . Cause: java.sql.SQLException: Incorrect string value: '\xE6\xB5\x8B\xE8\xAF\x95' for column 'name
- processing-MySQL example2-getString("StringName")
- 解决User.Identity.IsAuthenticated==false 或User.Identity.Name==string.empty的问题
- Type.GetType(string typeName) returns null !?
- 解决User.Identity.IsAuthenticated==false 或User.Identity.Name==string.empty的问题[转]
- [BTS] Error biztalk arguments null exception string reference not set to an instance of a string. parameter name
- Ibatis:There is no READABLE property named 'userName' in class 'java.lang.String'
- String 转换NameValueCollection 的方法 , 这是原创 嘎嘎
- 对于Name属性没有get/set方法引起的:target is null for setProperty(null, "x", [Ljava.lang.String;@1a48515)异常
- target is null for setProperty(null, "username", [Ljava.lang.String;@c06258)问题
- Incorrect string value: '\xF0\x9F\x98\x92' for column 'NIKENAME' at row 1;
- 解决User.Identity.IsAuthenticated=false 或User.Identity.Name=string.empty的问题
- 关于jstl EL用法的注意点(java.lang.NumberFormatException: For input string: "userName")
- 解决mysql插入数据时出现Incorrect string value: '\xF0\x9F...' for column 'name' at row 1的异常