Commit 9294cbc2 authored by Dr. Martin Goik's avatar Dr. Martin Goik

int to Double auto boxing exercise

parent 25719248
......@@ -139,69 +139,69 @@ int iPrimitiveFromInteger = <co linkends="sd1_numbers_fig_conventional-3"
</tr>
</informaltable>
<informaltable role="slideExclude" border="1">
<colgroup width="50%"/>
<colgroup width="50%"/>
<tr>
<th>Boxing and unboxing</th>
<th><quote>Conventional</quote> <xref linkend="glo_Java"/></th>
</tr>
<tr>
<td valign="top"><calloutlist>
<callout arearefs="sd1_numbers_fig_boxingAndUnboxing-1-co"
xml:id="sd1_numbers_fig_boxingAndUnboxing-1">
<para>Defining a primitive <code language="java">int</code>
value.</para>
</callout>
<callout arearefs="sd1_numbers_fig_boxingAndUnboxing-2-co"
xml:id="sd1_numbers_fig_boxingAndUnboxing-2">
<para>Creating an instance of <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html">Integer</classname>
by means of boxing.</para>
</callout>
<callout arearefs="sd1_numbers_fig_boxingAndUnboxing-3-co"
xml:id="sd1_numbers_fig_boxingAndUnboxing-3">
<para>Assigning an <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html">Integer</classname>'s
value to a primitive <code language="java">int</code> by means
of unboxing.</para>
</callout>
</calloutlist></td>
<td valign="top"><calloutlist>
<callout arearefs="sd1_numbers_fig_conventional-1-co"
xml:id="sd1_numbers_fig_conventional-1">
<para>Defining a primitive <code language="java">int</code>
value.</para>
</callout>
<callout arearefs="sd1_numbers_fig_conventional-2-co"
xml:id="sd1_numbers_fig_conventional-2">
<para>Creating a new instance of <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html">Integer</classname>
using the class method <methodname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html#valueOf-int-">Integer
valueOf​(int i)</methodname>.</para>
</callout>
<callout arearefs="sd1_numbers_fig_conventional-3-co"
xml:id="sd1_numbers_fig_conventional-3">
<para>Assigning an <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html">Integer</classname>'s
value to a primitive <code language="java">int</code> using the
<methodname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html#intValue--">int
intValue()</methodname> instance method.</para>
</callout>
</calloutlist></td>
</tr>
</informaltable>
<informaltable border="1" role="slideExclude">
<colgroup width="50%"/>
<colgroup width="50%"/>
<tr>
<th>Boxing and unboxing</th>
<th><quote>Conventional</quote> <xref linkend="glo_Java"/></th>
</tr>
<tr>
<td valign="top"><calloutlist>
<callout arearefs="sd1_numbers_fig_boxingAndUnboxing-1-co"
xml:id="sd1_numbers_fig_boxingAndUnboxing-1">
<para>Defining a primitive <code language="java">int</code>
value.</para>
</callout>
<callout arearefs="sd1_numbers_fig_boxingAndUnboxing-2-co"
xml:id="sd1_numbers_fig_boxingAndUnboxing-2">
<para>Creating an instance of <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html">Integer</classname>
by means of boxing.</para>
</callout>
<callout arearefs="sd1_numbers_fig_boxingAndUnboxing-3-co"
xml:id="sd1_numbers_fig_boxingAndUnboxing-3">
<para>Assigning an <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html">Integer</classname>'s
value to a primitive <code language="java">int</code> by means
of unboxing.</para>
</callout>
</calloutlist></td>
<td valign="top"><calloutlist>
<callout arearefs="sd1_numbers_fig_conventional-1-co"
xml:id="sd1_numbers_fig_conventional-1">
<para>Defining a primitive <code language="java">int</code>
value.</para>
</callout>
<callout arearefs="sd1_numbers_fig_conventional-2-co"
xml:id="sd1_numbers_fig_conventional-2">
<para>Creating a new instance of <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html">Integer</classname>
using the class method <methodname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html#valueOf-int-">Integer
valueOf​(int i)</methodname>.</para>
</callout>
<callout arearefs="sd1_numbers_fig_conventional-3-co"
xml:id="sd1_numbers_fig_conventional-3">
<para>Assigning an <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html">Integer</classname>'s
value to a primitive <code language="java">int</code> using
the <methodname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html#intValue--">int
intValue()</methodname> instance method.</para>
</callout>
</calloutlist></td>
</tr>
</informaltable>
</figure>
<figure xml:id="sd1_numbers_fig_stackBoxUnboxVsConventional">
......@@ -238,6 +238,43 @@ while (!values.empty()) {
</tr>
</informaltable>
</figure>
<qandaset defaultlabel="qanda"
xml:id="sd1_numbers_qanda_boxing_int2Double">
<title>Auto boxing int to Double?</title>
<qandadiv>
<qandaentry>
<question>
<para>Consider the following boxing conversion:</para>
<programlisting language="java">double d = 3.0;
Double dInstance = d;</programlisting>
<para>This code compiles and executes perfectly well. On contrary
the following snippet does not:</para>
<programlisting language="java">int i = 3;
Double dInstance = i;</programlisting>
<para>Explain this result. Hint: You may want to read <link
xlink:href="https://docs.oracle.com/javase/specs/jls/se12/html/jls-5.html">chapter
5</link> and <link
xlink:href="https://docs.oracle.com/javase/specs/jls/se12/html/jls-5.html#jls-5.2">section
5.2</link> in particular from the <link
xlink:href="https://docs.oracle.com/javase/specs/jls/se12/html/">The
Java® Language Specification</link>.</para>
</question>
<answer>
<para>The compiler will not perform auto boxing from
<code>int</code> do <code>Double</code>. On the other hand Double
is no subtype of Integer disallowing a widening reference
conversion.</para>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
<section xml:id="sd1_numbers_sect_numberParsing">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment