Commit 79010944 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Enhanced exercise + explanations

parent 74edfc65
......@@ -3789,103 +3789,188 @@ System.out.println("Maximum short value:" + MAXIMUM);</programlisting>
<qandadiv>
<qandaentry>
<question>
<para>Solution <xref linkend="sd1QandaBinaryIntLiteral"/>
contains:</para>
<orderedlist>
<listitem>
<para>Consider the following code snippet:</para>
<informaltable border="1">
<tr>
<th>Code</th>
<th>Output</th>
</tr>
<tr>
<td valign="top"><programlisting language="java">System.out.println("Result: " + 11 + 22);
System.out.println(11 + 22 + " is our result");</programlisting></td>
<td valign="top"><screen>Result: 1122
33 is our result</screen></td>
</tr>
</informaltable>
<para>Explain the different outcomes. </para>
<tip>
<para>Read about the <quote>+</quote> operator's role
<abbrev>e.g.</abbrev> in <code
language="java">System.out.println("Some" + "
string").</code></para>
</tip>
</listitem>
<listitem>
<para>The solution of <xref
linkend="sd1QandaBinaryIntLiteral"/> is related to a similar
problem:</para>
<programlisting language="java">...
<programlisting language="java">...
System.out.println("Decimal:" + (512 +
256 +
128 +
32 +
4)); ...</programlisting>
<para>Why are the <quote>inner</quote> braces immediately
preceding 512 and following 4 are being required?</para>
<para>Why are the <quote>inner</quote> braces grouping
<code>(512 + 256 + 128 + 4)</code> being required?</para>
</listitem>
</orderedlist>
<tip>
<itemizedlist>
<listitem>
<para>Execute the above code omitting the
<quote>inner</quote> braces.</para>
</listitem>
<listitem>
<para>Read about the <quote>+</quote> operator's role e.g.
in <code language="java">System.out.println("Some" + "
string").</code></para>
</listitem>
</itemizedlist>
<para>Execute the above code omitting these <quote>inner</quote>
braces.</para>
</tip>
</question>
<answer>
<para>We start omitting the <quote>inner</quote> braces:</para>
<orderedlist>
<listitem>
<para>We start by considering
<code>System.out.println("Result: " + 11 + 22)</code>.
Expressions involving the + operator are being evaluated from
left to right. The following two expressions are thus
equivalent:</para>
<programlisting language="java">...
System.out.println("Decimal:" + 512 +
<itemizedlist>
<listitem>
<para><code>"Result: " + 11 + 22</code></para>
</listitem>
<listitem>
<para><code>("Result: " + 11) + 22</code></para>
</listitem>
</itemizedlist>
<para>The integer literal 11 within <code>"Result: " +
11</code> will be converted into a string <code>"11"</code>
prior to being concatenated <code>with the "Result: "</code>
string into <code>a single "Result: 11"</code> string.</para>
<para>Likewise in a second step <code>the intermediate
"Result: 11"</code> string and the again converted
<code>"22"</code> will be concatenated into the final "Result:
1122" string.</para>
<para>Regarding <code>System.out.println(11 + 22 + " is our
result")</code> on the other hand the following two
expressions are equivalent:</para>
<itemizedlist>
<listitem>
<para><code>11 + 22 + " is our result"</code></para>
</listitem>
<listitem>
<para><code>(11 + 22) + " is our result"</code></para>
</listitem>
</itemizedlist>
<para>This time the expression <code>11 + 22</code> containing
<quote>pure</quote> integer arithmetics results in an integer
value of 33. This value will be turned into a "33" string
before being concatenated with <code>" is our result"</code>
into the final output string.</para>
</listitem>
<listitem>
<para>We start by omitting the <quote>inner</quote>
braces:</para>
<informaltable border="1">
<tr>
<th>Code</th>
<th>Output</th>
</tr>
<tr>
<td valign="top"><programlisting language="java">System.out.println("Decimal:" + <emphasis
role="red">512</emphasis> +
256 +
128 +
<emphasis role="red">128</emphasis> +
32 +
4); ...</programlisting>
<para>This results in the following output:</para>
<emphasis role="red">4</emphasis>); ...</programlisting></td>
<screen>...
Decimal:512256128324</screen>
<td valign="top"><screen>Decimal:<emphasis role="red">512</emphasis>256<emphasis
role="red">128</emphasis>32<emphasis role="red">4</emphasis></screen></td>
</tr>
</informaltable>
<para>The numbers are being treated as strings rather than integer
values The above code is equivalent to:</para>
<para>Like in this exercises first example the above code is
equivalent to:</para>
<programlisting language="java">...
<programlisting language="java">...
System.out.println("Decimal:" + "512" +
"256" +
"128" +
"32" +
"4"); ...</programlisting>
<para>The <quote>+</quote> operator between two strings defines
their concatenation. So all six components get joined into a
single result string.</para>
<para>Each <quote>+</quote> operator again defines the
concatenation of its left and right string operands rather
than the <quote>usual</quote> integer arithmetics. Thus all
six components get joined into a single result string.</para>
<para>Supplying additional inner braces defines an expression (512
+ 256 + 128 + 32 + 4) solely involving integer values. In this
context each <quote>+</quote> operator effects the usual integer
arithmetic:</para>
<para>Conversely supplying additional inner braces results in
an expression (512 + 256 + 128 + 32 + 4) solely involving
integer value arithmetics:</para>
<programlisting language="java">...
System.out.println("Decimal:" +<co linkends="sd1ListingPlusOpDuplicate-1"
xml:id="sd1ListingPlusOpDuplicate-1-co"/> (512 +<co
linkends="sd1ListingPlusOpDuplicate-2"
xml:id="sd1ListingPlusOpDuplicate-2-co"/>
<programlisting language="java">System.out.println("Decimal:" +<co
linkends="sd1ListingPlusOpDuplicate-1"
xml:id="sd1ListingPlusOpDuplicate-1-co"/> (512 +<co
linkends="sd1ListingPlusOpDuplicate-2"
xml:id="sd1ListingPlusOpDuplicate-2-co"/>
256 +<co
linkends="sd1ListingPlusOpDuplicate-2"
xml:id="sd1ListingPlusOpDuplicate-3-co"/>
linkends="sd1ListingPlusOpDuplicate-2"
xml:id="sd1ListingPlusOpDuplicate-3-co"/>
128 +<co
linkends="sd1ListingPlusOpDuplicate-2"
xml:id="sd1ListingPlusOpDuplicate-4-co"/>
linkends="sd1ListingPlusOpDuplicate-2"
xml:id="sd1ListingPlusOpDuplicate-4-co"/>
32 +<co
linkends="sd1ListingPlusOpDuplicate-2"
xml:id="sd1ListingPlusOpDuplicate-5-co"/>
linkends="sd1ListingPlusOpDuplicate-2"
xml:id="sd1ListingPlusOpDuplicate-5-co"/>
4)); ...</programlisting>
<calloutlist>
<callout arearefs="sd1ListingPlusOpDuplicate-1-co"
xml:id="sd1ListingPlusOpDuplicate-1">
<para><quote>+</quote> operator concatenating the two strings
<code language="java">"Decimal:"</code> and <code
language="java">"932"</code>.</para>
</callout>
<callout arearefs="sd1ListingPlusOpDuplicate-2-co sd1ListingPlusOpDuplicate-3-co sd1ListingPlusOpDuplicate-4-co sd1ListingPlusOpDuplicate-5-co"
xml:id="sd1ListingPlusOpDuplicate-2">
<para><quote>+</quote> operators computing the integer sum of
512, 256, 128, 32 and 4 yielding a value of 932. This value
subsequently gets transformed into the String <code
language="java">"932"</code> in order to be compatible with
the preceding <code language="java">"Decimal:"</code>
string.</para>
</callout>
</calloutlist>
<calloutlist>
<callout arearefs="sd1ListingPlusOpDuplicate-1-co"
xml:id="sd1ListingPlusOpDuplicate-1">
<para><quote>+</quote> operator concatenating the two
strings <code language="java">"Decimal:"</code> and <code
language="java">"932"</code>.</para>
</callout>
<callout arearefs="sd1ListingPlusOpDuplicate-2-co sd1ListingPlusOpDuplicate-3-co sd1ListingPlusOpDuplicate-4-co sd1ListingPlusOpDuplicate-5-co"
xml:id="sd1ListingPlusOpDuplicate-2">
<para><quote>+</quote> operators computing the integer sum
of 512, 256, 128, 32 and 4 yielding a value of 932. This
value subsequently gets transformed into the String <code
language="java">"932"</code> in order to be concatenated
with the preceding <code language="java">"Decimal:"</code>
string.</para>
</callout>
</calloutlist>
</listitem>
</orderedlist>
</answer>
</qandaentry>
</qandadiv>
......
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