我想從會話對象中讀取一個值,該值返回Object
類型。 我知道該對象具有true
/false
值。 我想將該值轉換爲boolean
類型。所以我有以下代碼。在Java中強制轉換.toString()和(String)的區別
session.getAttribute("MyKeyValue"); // It returns Object type
下面拋出異常。
boolean myBoolVal = Boolean.parseBoolean((String) session.getAttribute("MyKeyValue"));
下面的工作正常。
boolean myBoolVal = Boolean.parseBoolean(session.getAttribute("MyKeyValue").toString());
其實,我不明白爲什麼選項1不工作?
在這裏,這可能是劉曉丹的,太:http://stackoverflow.com/ question/328661/explicit-vs-implicit-call-of-tostring – GhostCat
session.getAttribute(「MyKeyValue」)。getClass()是什麼值? – yelliver