关于Java的Boolean.getBoolean方法和String转换Boolean的问题
今天读配置文件里面有Boolean类型的属性,之前都是 使用下面代码判断:
1 |
value.equals("true") |
感觉有点不优雅,于是用了一个Boolean类的静态方法Boolean.getBoolean,结果打断点一看:
1 |
Boolean.getBoolean("true"); // false |
返回false,有点莫名其妙。
断电进去看了一下getBoolean的源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/** * Returns {@code true} if and only if the system property * named by the argument exists and is equal to the string * {@code "true"}. (Beginning with version 1.0.2 of the * Java<small><sup>TM</sup></small> platform, the test of * this string is case insensitive.) A system property is accessible * through {@code getProperty}, a method defined by the * {@code System} class. * <p> * If there is no property with the specified name, or if the specified * name is empty or null, then {@code false} is returned. * * @param name the system property name. * @return the {@code boolean} value of the system property. * @see java.lang.System#getProperty(java.lang.String) * @see java.lang.System#getProperty(java.lang.String, java.lang.String) */ public static boolean getBoolean(String name) { boolean result = false; try { result = toBoolean(System.getProperty(name)); } catch (IllegalArgumentException e) { } catch (NullPointerException e) { } return result; } |
呃,看到有个 System.getProperty(name) 感觉不对劲
看一下注释原来这个方法
当且仅当以参数命名的系统属性存在,且等于 “true” 字符串时,才返回 true。(从 JavaTM 1.0.2 平台开始,字符串的测试不再区分大
小写。)通过 getProperty 方法可访问系统属性,此方法由 System 类定义。
如果没有以指定名称命名的属性或者指定名称为空或 null,则返回 false。
System.getProperty都有哪些呢?跟一下断点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/** * System properties. The following properties are guaranteed to be defined: * <dl> * <dt>java.version <dd>Java version number * <dt>java.vendor <dd>Java vendor specific string * <dt>java.vendor.url <dd>Java vendor URL * <dt>java.home <dd>Java installation directory * <dt>java.class.version <dd>Java class version number * <dt>java.class.path <dd>Java classpath * <dt>os.name <dd>Operating System Name * <dt>os.arch <dd>Operating System Architecture * <dt>os.version <dd>Operating System Version * <dt>file.separator <dd>File separator ("/" on Unix) * <dt>path.separator <dd>Path separator (":" on Unix) * <dt>line.separator <dd>Line separator ("\n" on Unix) * <dt>user.name <dd>User account name * <dt>user.home <dd>User home directory * <dt>user.dir <dd>User's current working directory * </dl> */ private static Properties props; |
只有发的参数是上面的字符串并且该属性的值为true时 才会返回 true
呃,那么怎么才能把字符串转成Boolean呢? 继续看Boolean源码,发现私有方法:
1 2 3 |
private static boolean toBoolean(String name) { return ((name != null) && name.equalsIgnoreCase("true")); } |
那么都有哪些方法调用它呢?
构造方法:
1 2 3 |
public Boolean(String s) { this(toBoolean(s)); } |
其他方法:
1 2 3 |
public static boolean parseBoolean(String s) { return toBoolean(s); } |
1 2 3 |
public static Boolean valueOf(String s) { return toBoolean(s) ? TRUE : FALSE; } |
所以类型转换还是使用paese开头的方法才是正确的姿态。不看API容易进坑啊。
本文链接:关于Java的Boolean.getBoolean方法和String转换Boolean的问题
转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:破晓(http://www.code2048.net),谢谢!^^