//摘自ocjp
public void testIfA() {
if (testIfB("True")) {
System.out.println("True");
} else {
System.out.println("Not true");
}
}
public Boolean testIfB(String str) {
return Boolean.valueOf(str);
}
What is the result when method testIfA is invoked?
A. True
B. Not true
C. An exception is thrown at runtime.
D. Compilation fails because of an error at line 12.
E. Compilation fails because of an error at line 19.
Answer: A
分析:
查看API:
//摘自Jdk1.6.21 API--Boolean类。
public static Boolean valueOf(String s) {
return toBoolean(s) ? TRUE : FALSE;
}
private static boolean toBoolean(String name) {
return ((name != null) && name.equalsIgnoreCase("true"));
}
不难看出,此处不区分大小写。
本文通过一个简单的Java示例展示了如何使用Boolean类的valueOf方法来转换字符串为布尔值,并解释了该过程中的大小写敏感性问题。
1578

被折叠的 条评论
为什么被折叠?



