上面的代碼中,為什么b2為false,而不是true
有關Boolean類
qq_不睡覺的怪叔叔_0
2016-07-28 15:57:54
TA貢獻34條經驗 獲得超34個贊
????關于這個問題樓主可以參考一下Java的API文檔,文檔中對Boolean(String?s) 的是這樣描述的:?如果 String 參數不為 null 且在忽略大小寫時等于 "true",則分配一個表示 true 值的 Boolean 對象(如下圖)。

下面給出一個提調試運行過的簡單的例子。
源代碼:
public?class?Main
{
public?static?void?main(String[]?args){
?Boolean?b1?=?new?Boolean("true");
?Boolean?b2?=?new?Boolean("tRUe");
?Boolean?b3?=?new?Boolean("yes");
?System.out.println("b1?=?"+b1);
?System.out.println("b2?=?"+b2);
?System.out.println("b3?=?"+b3);
}
}輸出結果:
b1?=?true b2?=?true b3?=?false
舉報