Java type-casting examples
<p>I have an exam soon and I want someone to check my explanations why some conversions are allowed by the compiler, and some aren't.</p>

<p>byte--> short --> int --> long --> float --> double</p>

<p>So I can always cast (unexplicitly) from small to big i.e. from byte to long for example. But to go in this direction &lt;-- I have to explicitly typecast. So far so good, but questions at the exam are still kinda tricky.</p>

<pre><code>int value1 = (double) 10;
</code></pre>

<p>Not allowed. Eclipse says cannot convert from double to int. But isn't 10 an integer already?? I don't really get it.</p>

<pre><code>byte value2 = (int) 120.0;
</code></pre>

<p>Allowed. So the 120.0 gets cut to 120 which is then an integer which is within the range of a byte. But for example the follwing</p>

<pre><code>byte value3 = (int) 33500.0;
</code></pre>

<p>is not allowed. Because it's not within the range of a byte?</p>

<pre><code>double value4 = (int) 13.2;
</code></pre>

<p>Allowed. 13.2 gets cut to 13 which is an integer. You can always assign small to big.</p>

<pre><code>int value = 10f;
</code></pre>

<p>not allowed. Because you cannot convert from float to int, but however</p>

<pre><code>int value = (int) 10f
</code></pre>

<p>is allowed, since now it's casted explicitly.</p>

<pre><code>double someValue = (int) 12.3;
</code></pre>

<p>Allowed, I guess. But this doesn't make a lot of sense, does it? Since 12.3 is already a double.</p>

<pre><code>short v1 = (int) 42.0;
</code></pre>

<p>Allowed. But Why?? I thought you cannot go in this &lt;-- direction.</p>