What is difference between .toString() and (String) cast in Java
<p>I want to read a value from the session object which returns <code>Object</code> type.
I know that the object has either <code>true</code>/<code>false</code> value.
I would like to convert that value into <code>boolean</code> type. So I have the following code.</p>

<pre><code>session.getAttribute("MyKeyValue"); // It returns Object type
</code></pre>

<p>Below throws an Exception.</p>

<pre><code>boolean myBoolVal = Boolean.parseBoolean((String) session.getAttribute("MyKeyValue"));
</code></pre>

<p>Below works properly.</p>

<pre><code>boolean myBoolVal = Boolean.parseBoolean(session.getAttribute("MyKeyValue").toString());
</code></pre>

<p>Actually, I don't understand why option 1 is not working ? </p>