今天读配置文件里面有Boolean类型的属性,之前都是 使用下面代码判断:
Java
value.equals("true")
1
value.equals("true")
感觉有点不优雅,于是用了一个Boolean类的静态方法Boolean.getBoolean,结果打断点一看:
Java
Boolean.getBoolean("true"); // false
1
Boolean.getBoolean("true");// false
返回false,有点莫名其妙。
断电进去看了一下getBoolean的源码:
Java
/**
* 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
* JavaTM 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.
*
* 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;
}
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
* JavaTM 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.
*
* 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)
*/
publicstaticbooleangetBoolean(Stringname){
booleanresult=false;
try{
result=toBoolean(System.getProperty(name));
}catch(IllegalArgumentExceptione){
}catch(NullPointerExceptione){
}
returnresult;
}
呃,看到有个 System.getProperty(name) 感觉不对劲
看一下注释原来这个方法
当且仅当以参数命名的系统属性存在,且等于 “true” 字符串时,才返回 true。(从 JavaTM 1.0.2 平台开始,字符串的测试不再区分大
小写。)通过 getProperty 方法可访问系统属性,此方法由 System 类定义。
如果没有以指定名称命名的属性或者指定名称为空或 null,则返回 false。
System.getProperty都有哪些呢?跟一下断点:
Java
/**
* System properties. The following properties are guaranteed to be defined:
*
*
java.version Java version number*
java.vendor Java vendor specific string*
java.vendor.url Java vendor URL*
java.home Java installation directory*
java.class.version Java class version number*
java.class.path Java classpath*
os.name Operating System Name*
os.arch Operating System Architecture*
os.version Operating System Version*
file.separator File separator ("/" on Unix)*
path.separator Path separator (":" on Unix)*
line.separator Line separator ("\n" on Unix)*
user.name User account name*
user.home User home directory*
user.dir User's current working directory*
*/
private static Properties props;
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:
*
*
java.version Java version number*
java.vendor Java vendor specific string*
java.vendor.url Java vendor URL*
java.home Java installation directory*
java.class.version Java class version number*
java.class.path Java classpath*
os.name Operating System Name*
os.arch Operating System Architecture*
os.version Operating System Version*
file.separator File separator ("/" on Unix)*
path.separator Path separator (":" on Unix)*
line.separator Line separator ("\n" on Unix)*
user.name User account name*
user.home User home directory*
user.dir User's current working directory*
*/
privatestaticPropertiesprops;
只有发的参数是上面的字符串并且该属性的值为true时 才会返回 true
呃,那么怎么才能把字符串转成Boolean呢? 继续看Boolean源码,发现私有方法:
Java
private static boolean toBoolean(String name) {
return ((name != null) && name.equalsIgnoreCase("true"));
}
1
2
3
privatestaticbooleantoBoolean(Stringname){
return((name!=null)&&name.equalsIgnoreCase("true"));
}
那么都有哪些方法调用它呢?
构造方法:
Java
public Boolean(String s) {
this(toBoolean(s));
}
1
2
3
publicBoolean(Strings){
this(toBoolean(s));
}
其他方法:
Java
public static boolean parseBoolean(String s) {
return toBoolean(s);
}
1
2
3
publicstaticbooleanparseBoolean(Strings){
returntoBoolean(s);
}
Java
public static Boolean valueOf(String s) {
return toBoolean(s) ? TRUE : FALSE;
}
1
2
3
publicstaticBooleanvalueOf(Strings){
returntoBoolean(s)?TRUE:FALSE;
}
所以类型转换还是使用paese开头的方法才是正确的姿态。不看API容易进坑啊。