diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml index 0a5981f872e9fc0b15a2dde55af041c423c49aa2..bddb6344e136062bd4c39913cad5a24633953e8e 100644 --- a/Doc/Sd1/languageFundamentals.xml +++ b/Doc/Sd1/languageFundamentals.xml @@ -325,8 +325,8 @@ X X X X X</programlisting> <programlisting language="java">double pi;</programlisting> </figure> - <para>We may assign values to variables or build expressions like <code language="java">pi - * 2.0 * 2.0</code> :</para> + <para>We may assign values to variables or build expressions like <code + language="java">pi * 2.0 * 2.0</code> :</para> <figure xml:id="sd1_fig_varDeclareAndUse"> <title>Declare and use</title> @@ -394,13 +394,14 @@ int c;</programlisting> <itemizedlist> <listitem> - <para>Start with a small letter like <code language="java">africa</code> rather than - <code language="java">Africa</code>.</para> + <para>Start with a small letter like <code + language="java">africa</code> rather than <code + language="java">Africa</code>.</para> </listitem> <listitem> - <para>Use <quote>camel case</quote> e.g. - <code language="java">myFirstCode</code>.</para> + <para>Use <quote>camel case</quote> e.g. <code + language="java">myFirstCode</code>.</para> <mediaobject> <imageobject> @@ -410,7 +411,8 @@ int c;</programlisting> </listitem> <listitem> - <para>Do not start with <code language="java">_</code> or <code language="java">$</code>.</para> + <para>Do not start with <code language="java">_</code> or <code + language="java">$</code>.</para> </listitem> </itemizedlist> </figure> @@ -444,14 +446,15 @@ pi = 1.0; // Compile time error: Constant cannot be modified</programlisting> <tr> <td><itemizedlist> <listitem> - <para>Start with a letter, <quote><code language="java">_</code></quote> or - <quote><code language="java">$</code></quote></para> + <para>Start with a letter, <quote><code + language="java">_</code></quote> or <quote><code + language="java">$</code></quote></para> </listitem> <listitem> <para><emphasis role="bold">May</emphasis> be followed by - letters, digits, <quote><code language="java">_</code></quote> or - <quote><code language="java">$</code></quote></para> + letters, digits, <quote><code language="java">_</code></quote> + or <quote><code language="java">$</code></quote></para> </listitem> <listitem> @@ -464,8 +467,9 @@ pi = 1.0; // Compile time error: Constant cannot be modified</programlisting> </listitem> <listitem> - <para><quote><code language="java">boolean</code></quote> or - <quote><code language="java">null</code></quote> literal</para> + <para><quote><code language="java">boolean</code></quote> + or <quote><code language="java">null</code></quote> + literal</para> </listitem> </itemizedlist> </listitem> @@ -491,7 +495,8 @@ pi = 1.0; // Compile time error: Constant cannot be modified</programlisting> <td valign="top"><itemizedlist> <listitem> - <para><code language="java"><emphasis role="red">2sad</emphasis></code></para> + <para><code language="java"><emphasis + role="red">2sad</emphasis></code></para> </listitem> <listitem> @@ -500,7 +505,8 @@ pi = 1.0; // Compile time error: Constant cannot be modified</programlisting> </listitem> <listitem> - <para><code language="java"><emphasis role="red">true</emphasis></code></para> + <para><code language="java"><emphasis + role="red">true</emphasis></code></para> </listitem> </itemizedlist></td> </tr> @@ -758,8 +764,8 @@ System.out.println(count + ":" + Count);</programlisting> <td>yes</td> - <td><code language="java">println</code> is a method of - <code language="java">System.out.println(...)</code> but is no <xref + <td><code language="java">println</code> is a method of <code + language="java">System.out.println(...)</code> but is no <xref linkend="glo_Java"/> keyword.</td> </tr> @@ -1000,18 +1006,19 @@ int i = d; // Error: Incompatible types</programlisting> <answer> <para>The first snippet compiles by using the <emphasis - role="bold">widening conversion</emphasis>: When assigning <code language="java">d - = i</code> the <xref linkend="glo_Java"/> compiler implicitly - converts the <code language="java" + role="bold">widening conversion</emphasis>: When assigning <code + language="java">d = i</code> the <xref linkend="glo_Java"/> + compiler implicitly converts the <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> into a <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">double</code> value.</para> <para>Turning a double into an int is more cumbersome: The - expression <code language="java">i = 3.5</code> could be evaluated by agreeing on - a specific rounding prescription. But what about i = - 3457357385783573478955345.45? A <xref linkend="glo_Java"/> <code language="java" + expression <code language="java">i = 3.5</code> could be evaluated + by agreeing on a specific rounding prescription. But what about i + = 3457357385783573478955345.45? A <xref linkend="glo_Java"/> <code + language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code>'s maximum value is <inlineequation> <m:math display="inline"> @@ -1331,7 +1338,8 @@ int i = (int) d; // Explicit cast double to int</programlisting> <para>In 3-bit two complement representation the leftmost bit will be discarded. Thus the actual result - is <code language="java">101</code> as being expected.</para> + is <code language="java">101</code> as being + expected.</para> </glossdef> </glossentry> @@ -1437,13 +1445,15 @@ int i = (int) d; // Explicit cast double to int</programlisting> </tr> <tr> - <td><code language="java">00000000_00000000_00000000_00000000</code></td> + <td><code + language="java">00000000_00000000_00000000_00000000</code></td> <td>0</td> </tr> <tr> - <td><code language="java">01111111_11111111_11111111_11111111</code></td> + <td><code + language="java">01111111_11111111_11111111_11111111</code></td> <td><inlineequation> <m:math display="inline"> @@ -1469,7 +1479,8 @@ int i = (int) d; // Explicit cast double to int</programlisting> </tr> <tr> - <td><code language="java">10000000_00000000_00000000_00000000</code></td> + <td><code + language="java">10000000_00000000_00000000_00000000</code></td> <td><inlineequation> <m:math display="inline"> @@ -1493,21 +1504,24 @@ int i = (int) d; // Explicit cast double to int</programlisting> </tr> <tr> - <td><code language="java">11111111_11111111_11111111_11111111</code></td> + <td><code + language="java">11111111_11111111_11111111_11111111</code></td> <td>-1</td> </tr> </table> <para>Use <link - xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1"><code language="java" + xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1"><code + language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> literals</link> in <link xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-BinaryNumeral">binary - representation</link> like e.g. <code language="java">0B1100</code> in the - <quote>Language Fundamentals</quote> / <quote>Literals</quote> - section of <xref linkend="bib_Kurniawan2015"/> in order to write - an <code language="java" + representation</link> like e.g. <code + language="java">0B1100</code> in the <quote>Language + Fundamentals</quote> / <quote>Literals</quote> section of <xref + linkend="bib_Kurniawan2015"/> in order to write an <code + language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code>'s minimum and maximum possible value to standard output.</para> @@ -1538,7 +1552,8 @@ int i = (int) d; // Explicit cast double to int</programlisting> }</programlisting> <para>BTW: The <xref linkend="glo_JDK"/> does provide maximum - value, minimum value and related information for <code language="java" + value, minimum value and related information for <code + language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code>, <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-A">byte</code>, @@ -1746,8 +1761,9 @@ int a = 20, <para>The above code does not compile due to a compiler error when defining variable <code language="java">c</code>.</para> - <para>Explain the underlying cause. In particular: Why is <code language="java">b - = 03</code> just fine in contrast to <code language="java">c = 09</code> ?</para> + <para>Explain the underlying cause. In particular: Why is <code + language="java">b = 03</code> just fine in contrast to <code + language="java">c = 09</code> ?</para> <tip> <para>Re-read the section on integer literal @@ -1775,8 +1791,8 @@ int a = 20, <programlisting language="java">int a = 041; System.out.println("Value = " + a);</programlisting> - <para>On execution we receive the output <code language="java">Value = 33</code>. - Explain this result</para> + <para>On execution we receive the output <code + language="java">Value = 33</code>. Explain this result</para> </question> <answer> @@ -1820,8 +1836,8 @@ System.out.println("Value = " + a);</programlisting> <tip> <para>Try to assign the corresponding int values like e.g. - <code language="java">0x27BD</code> rather than using the symbol - itself.</para> + <code language="java">0x27BD</code> rather than using the + symbol itself.</para> </tip> </listitem> @@ -1836,19 +1852,21 @@ System.out.println("Value = " + a);</programlisting> <orderedlist> <listitem> <para>Our emoticon's decimal <xref linkend="glo_unicode"/> - value is 128,526 or hexadecimal <code language="java">1f60E</code> . So its - value is larger than 65535 and thus requires more than two - bytes. <xref linkend="glo_Java"/> however represents - characters using just two bytes. Thus the emoticon does not - fit into a single <xref linkend="glo_Java"/> <code language="java">char</code> + value is 128,526 or hexadecimal <code + language="java">1f60E</code> . So its value is larger than + 65535 and thus requires more than two bytes. <xref + linkend="glo_Java"/> however represents characters using just + two bytes. Thus the emoticon does not fit into a single <xref + linkend="glo_Java"/> <code language="java">char</code> value:</para> <programlisting language="java">char c = 0x1f60e; // Error: Incompatible types // Required: char Found: int</programlisting> <para>On contrary the arrow's decimal value of 10173 or - hexadecimal <code language="java">0x27BD</code> fits perfectly well into a - <xref linkend="glo_Java"/> <code language="java">char</code> variable:</para> + hexadecimal <code language="java">0x27BD</code> fits perfectly + well into a <xref linkend="glo_Java"/> <code + language="java">char</code> variable:</para> <programlisting language="java">char c = 0x27BD; System.out.println("Arrow: " + c); @@ -2175,51 +2193,59 @@ Value 4: 29</screen> <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code></td> - <td><code language="java">'A'</code>, <code language="java">'\u0041'</code></td> + <td><code language="java">'A'</code>, <code + language="java">'\u0041'</code></td> </tr> <tr> <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code></td> - <td><code language="java">29</code>, <code language="java">0b1_1101</code>, <code language="java">0x1D</code>, - <code language="java">035</code>, <code language="java">-29</code>,</td> + <td><code language="java">29</code>, <code + language="java">0b1_1101</code>, <code language="java">0x1D</code>, + <code language="java">035</code>, <code + language="java">-29</code>,</td> </tr> <tr> <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code></td> - <td><code language="java">35<emphasis role="red">L</emphasis></code>, - <code language="java">0b10_0011<emphasis role="red">L</emphasis></code>, + <td><code language="java">35<emphasis + role="red">L</emphasis></code>, <code + language="java">0b10_0011<emphasis role="red">L</emphasis></code>, <code language="java">0x23<emphasis role="red">L</emphasis></code>, <code language="java">043<emphasis role="red">L</emphasis></code>, - <code language="java">-35<emphasis role="red">L</emphasis></code>,...</td> + <code language="java">-35<emphasis + role="red">L</emphasis></code>,...</td> </tr> <tr> <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">float</code></td> - <td><code language="java">55.43<emphasis role="red">F</emphasis></code>, - <code language="java">1.7E-23<emphasis role="red">F</emphasis></code>, + <td><code language="java">55.43<emphasis + role="red">F</emphasis></code>, <code + language="java">1.7E-23<emphasis role="red">F</emphasis></code>, <code language="java">-17.<emphasis role="red">F</emphasis></code>, - <code language="java">100_342.334_113<emphasis role="red">F</emphasis></code></td> + <code language="java">100_342.334_113<emphasis + role="red">F</emphasis></code></td> </tr> <tr> <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">double</code></td> - <td><code language="java">55.43</code>, <code language="java">1.7 E -23</code>, - <code language="java">-17.</code></td> + <td><code language="java">55.43</code>, <code language="java">1.7 E + -23</code>, <code language="java">-17.</code></td> </tr> <tr> <td><code language="java" xlink:href="https://docs.oracle.com/javase/tutorial/java/data/strings.html">String</code></td> - <td><code language="java">"Hello"</code>, <code language="java">"Greek Δ"</code>, <code language="java">"Greek <link + <td><code language="java">"Hello"</code>, <code + language="java">"Greek Δ"</code>, <code language="java">"Greek <link xlink:href="https://www.fileformat.info/info/unicode/char/0394/index.htm">\u0394</link>"</code></td> </tr> @@ -2252,10 +2278,11 @@ Value 4: 29</screen> xlink:href="http://www.rapidtables.com/code/text/ascii-table.htm#print">printable ASCII characters</link>. Write a <xref linkend="glo_Java"/> application by starting just from from the character literals - <code language="java">' '</code>, <code language="java">'!'</code>, <code language="java">'"'</code>, - <code language="java">'#'</code>, '$', '%', '&' to show their corresponding - <xref linkend="glo_ASCII"/> decimal values. The intended output - is:</para> + <code language="java">' '</code>, <code + language="java">'!'</code>, <code language="java">'"'</code>, + <code language="java">'#'</code>, '$', '%', '&' to show their + corresponding <xref linkend="glo_ASCII"/> decimal values. The + intended output is:</para> <screen> : 32 !: 33 @@ -2271,7 +2298,8 @@ $: 36 <tip> <para>A <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code> - value being represented by two bytes may be assigned to an <code language="java" + value being represented by two bytes may be assigned to an <code + language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> variable.</para> </tip> @@ -2532,10 +2560,10 @@ Decimal:932</screen> linkend="sw1QandaMaxMinInt">related int example</link> accordingly:</para> - <programlisting language="java">short minumum = 0B10000000_00000000, + <programlisting language="java">short minimum = 0B10000000_00000000, // short: two byte two-complement maximum = 0B01111111_11111111; -System.out.println("Minimum short value:" + minumum); +System.out.println("Minimum short value:" + minimum); System.out.println("Maximum short value:" + maximum</programlisting> <para>Our programmer is baffled:</para> @@ -2551,7 +2579,8 @@ System.out.println("Maximum short value:" + maximum</programlisting> </listitem> <listitem> - <para>On contrary the second assignment <code language="java">maximum = 0B01111111_11111111</code> gets + <para>On contrary the second assignment <code + language="java">maximum = 0B01111111_11111111</code> gets smoothly accepted.</para> </listitem> </itemizedlist> @@ -2566,8 +2595,8 @@ System.out.println("Maximum short value:" + maximum</programlisting> <answer> <para>Since variables of type <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code> - have a two bytes representation their corresponding range is - <inlineequation> + are signed having a two byte two complement representation their + corresponding range is <inlineequation> <m:math display="inline"> <m:mrow> <m:mo>[</m:mo> @@ -2604,24 +2633,90 @@ System.out.println("Maximum short value:" + maximum</programlisting> </inlineequation>. Thus intensity values ranging from 0 to 255 will be easily accommodated.</para> - <para>The second question is more difficult to explain: The <xref + <para>In fact using a <code language="java">byte</code> might be + sufficient as well: Our programmer simply needs to map the + intended intensity values <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>[</m:mo> + + <m:mrow> + <m:mi>0</m:mi> + + <m:mo>,</m:mo> + + <m:mrow> + <m:msup> + <m:mi>2</m:mi> + + <m:mi>8</m:mi> + </m:msup> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> + + <m:mo>]</m:mo> + </m:mrow> + </m:math> + </inlineequation> to a <code language="java">byte</code>'s range + of <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>[</m:mo> + + <m:mrow> + <m:mrow> + <m:mo>-</m:mo> + + <m:msup> + <m:mi>2</m:mi> + + <m:mi>7</m:mi> + </m:msup> + </m:mrow> + + <m:mo>,</m:mo> + + <m:mrow> + <m:msup> + <m:mi>2</m:mi> + + <m:mi>7</m:mi> + </m:msup> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> + + <m:mo>]</m:mo> + </m:mrow> + </m:math> + </inlineequation>.</para> + + <para>The second question is more difficult to answer: The <xref linkend="glo_Java"/> standard only defines <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> - literals. It does however not define <code language="java" + but no <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code> - (or <code language="java" - xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-A">byte</code>) + or <code language="java" + xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-A">byte</code> literals.</para> - <para>Thus the 0B01111111_11111111 literal will be treated as an + <para>Thus our 0B01111111_11111111 literal will be treated as an <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> - value being compatible with a <code language="java" + value being assignable to a <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code> - variable and is thus assignable.</para> + variable.</para> - <para>On the contrary the binary value - <code language="java">10000000_00000000</code> evaluates to <inlineequation> + <para>On the contrary the binary <code language="java">int</code> + value <code language="java">10000000_00000000</code> evaluates to + <inlineequation> <m:math display="inline"> <m:msup> <m:mn>2</m:mn> @@ -2629,13 +2724,28 @@ System.out.println("Maximum short value:" + maximum</programlisting> <m:mi>15</m:mi> </m:msup> </m:math> - </inlineequation> (equal to 32768) exceeding a two-byte (signed) - short variable's upper limit.</para> + </inlineequation> (equal to 32768) rather then being interpreted + as a two byte two complement <code language="java">short</code>. + It thus exceeds a <code language="java">short</code> variable's + upper limit being totally different from the intended two-byte two + complement short value of <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> + + <m:msup> + <m:mi>2</m:mi> + + <m:mi>15</m:mi> + </m:msup> + </m:mrow> + </m:math> + </inlineequation>.</para> <para>Even worse: A <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code> - variable containing binary <code language="java">10000000_00000000</code> due to - its two byte <link + variable containing binary <code + language="java">10000000_00000000</code> due to its two byte <link xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's complement</link> representation has got a value <inlineequation> <m:math display="inline"> @@ -2651,10 +2761,22 @@ System.out.println("Maximum short value:" + maximum</programlisting> </m:math> </inlineequation> (or -32768).</para> - <para>In order to avoid this error we need an explicit type - conversion (a so called cast):</para> + <para>Avoiding this error requires the value <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> + + <m:msup> + <m:mn>2</m:mn> - <programlisting language="java">short minumum = (short) 0B10000000_00000000;</programlisting> + <m:mi>15</m:mi> + </m:msup> + </m:mrow> + </m:math> + </inlineequation> in <code language="java">int</code> + notation:</para> + + <programlisting language="java">short minumum = 0B11111111_11111111_10000000_00000000;</programlisting> <para>Since a <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code> @@ -2692,14 +2814,20 @@ System.out.println("Maximum short value:" + maximum</programlisting> <m:mo>]</m:mo> </m:mrow> </m:math> - </inlineequation> we may as well use a unary minus sign - instead:</para> + </inlineequation> we may as well use int <inlineequation> + <m:math display="inline"> + <m:msup> + <m:mn>2</m:mn> + + <m:mi>15</m:mi> + </m:msup> + </m:math> + </inlineequation> adding a unary minus sign:</para> <programlisting language="java">short minumum = -0B10000000_00000000;</programlisting> <para>Both variants are syntactically correct and will thus - compile. However they are likely not being intended by our - programmer.</para> + compile.</para> <para>The error in the first place is probably assigning a value of <inlineequation> @@ -2716,15 +2844,16 @@ System.out.println("Maximum short value:" + maximum</programlisting> </m:math> </inlineequation> to a <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code> - variable at all.</para> + variable mixing two- and four-byte two-complement + representation.</para> </answer> </qandaentry> </qandadiv> </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1QandaBracesInPrintln"> - <title>Why using braces inside <code language="java">System.out.println(...)</code> - ?</title> + <title>Why using braces inside <code + language="java">System.out.println(...)</code> ?</title> <qandadiv> <qandaentry> @@ -2812,16 +2941,18 @@ System.out.println("Decimal:" +<co linkends="sd1ListingPlusOpDuplicate-1" <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> + <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> + 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> </answer> @@ -2848,9 +2979,9 @@ System.out.println("Decimal:" +<co linkends="sd1ListingPlusOpDuplicate-1" <screen>3 Games having 22 players each results in 66 players altogether.</screen> - <para>Write your code in a way that changing i.e. <code language="java">final int - games = 4</code> will result in a corresponding change of - output.</para> + <para>Write your code in a way that changing i.e. <code + language="java">final int games = 4</code> will result in a + corresponding change of output.</para> </question> <answer> @@ -2934,18 +3065,18 @@ System.out.println("Decimal:" +<co linkends="sd1ListingPlusOpDuplicate-1" <itemizedlist> <listitem> - <para - xlink:href="http://codingbat.com/prob/p171896">helloName</para> + <para><link + xlink:href="http://codingbat.com/prob/p171896">helloName</link></para> </listitem> <listitem> - <para - xlink:href="http://codingbat.com/prob/p161056">makeAbba</para> + <para><link + xlink:href="http://codingbat.com/prob/p161056">makeAbba</link></para> </listitem> <listitem> - <para - xlink:href="http://codingbat.com/prob/p147483">makeTags</para> + <para xlink:href="http://codingbat.com/prob/p147483"><link + xlink:href="http://codingbat.com/prob/p147483">makeTags</link></para> </listitem> </itemizedlist> </question> @@ -2991,8 +3122,9 @@ System.out.println("Character: " + arabicChar);</programlisting> <answer> <para>According to the <link xlink:href="https://en.wikipedia.org/wiki/Arabic_(Unicode_block)#Block">Arabic - block</link> table the code point of <code language="java">'ڜ'</code> is U+069C - having a decimal equivalent of 1692.</para> + block</link> table the code point of <code + language="java">'ڜ'</code> is U+069C having a decimal equivalent + of 1692.</para> <para><xref linkend="glo_Java"/> uses two bytes equivalent to 16 bits for representing positive <code language="java" @@ -3018,8 +3150,8 @@ System.out.println("Character: " + arabicChar);</programlisting> <m:mi>65535</m:mi> </m:mrow> </m:math> - </inlineequation>. So <code language="java">'ڜ'</code> fits well into this - range.</para> + </inlineequation>. So <code language="java">'ڜ'</code> fits well + into this range.</para> <para>According to the <link xlink:href="https://en.wikipedia.org/wiki/Emoticons_(Unicode_block)">Emoticons @@ -3087,11 +3219,12 @@ _____________________________________ </figure> <para>This exceeds the four byte limit of int variables. Thus the leading - <quote>1</quote> will be discarded leaving us with a result of - <code language="java">11111111_11111111_11111111_11111110</code> equalling -2 with respect - to four byte integer two complement representation.</para> + <quote>1</quote> will be discarded leaving us with a result of <code + language="java">11111111_11111111_11111111_11111110</code> equalling -2 + with respect to four byte integer two complement representation.</para> - <para>Why not using float / double in favour of bounded <code language="java" + <para>Why not using float / double in favour of bounded <code + language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-A">byte</code>, <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code>, @@ -3266,7 +3399,7 @@ System.out.println(Integer.toBinaryString(64200)); // Yields 11111010_11001000 </qandaset> <qandaset defaultlabel="qanda" xml:id="qandaNarrowing137toByte"> - <title>Get a <code language="java">byte</code> from 137</title> + <title>Get a <code language="java">byte</code> from 139</title> <qandadiv> <qandaentry> @@ -3284,13 +3417,14 @@ System.out.println(b);</programlisting> <answer> <para>A four byte <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> - representation of 139 reads - <code language="java">00000000_00000000_00000000_10001011</code>. The cast <code language="java">b - = (byte) i</code> will strip the leading three bytes leaving - <code language="java">b</code> containing <code language="java">10001011</code>.</para> + representation of 139 reads <code + language="java">00000000_00000000_00000000_10001011</code>. The + cast <code language="java">b = (byte) i</code> will strip the + leading three bytes leaving us with <code language="java">b</code> + containing <code language="java">10001011</code>.</para> <para>Since byte values in <xref linkend="glo_Java"/> are being - represented as signed values using two-complement notation this + represented as signed values in two-complement notation this equals decimal -117.</para> </answer> </qandaentry> @@ -3310,9 +3444,10 @@ System.out.println(b);</programlisting> mimic a code portion in <xref linkend="glo_Java"/> showing the catastrophic error.</para> - <para>Start with a <code language="java">double</code> variable using a value - being suitable to be assigned to a <code language="java">short</code> variable - using a cast (narrowing).</para> + <para>Start with a <code language="java">double</code> variable + using a value being suitable to be assigned to a <code + language="java">short</code> variable using a cast + (narrowing).</para> <para>Then in a second step raise this value breaking your short variable's upper limit.</para> @@ -3329,9 +3464,9 @@ System.out.println(value);</programlisting> <para>Execution yields an expected integer output of <computeroutput>2331</computeroutput>. However increasing our - <code language="java">level</code> variable's value from 2331.12 to 42331.12 - yields an output of <computeroutput>-23205</computeroutput> due to - an overflow.</para> + <code language="java">level</code> variable's value from 2331.12 + to 42331.12 yields an output of + <computeroutput>-23205</computeroutput> due to an overflow.</para> </answer> </qandaentry> </qandadiv> @@ -3352,7 +3487,8 @@ System.out.println(value);</programlisting> xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> values to <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> - values. This requires scaling down <quote>half</quote> the <code language="java" + values. This requires scaling down <quote>half</quote> the <code + language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> data type's range <inlineequation> <m:math display="inline"> @@ -3660,7 +3796,8 @@ System.out.println(value);</programlisting> <m:mi>32</m:mi> </m:msup> </m:math> - </inlineequation> seems to be equal to <code language="java">2 * (<link + </inlineequation> seems to be equal to <code language="java">2 * + (<link xlink:href="https://docs.oracle.com/javase/9/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</link> + 1))</code> (why?) Joe's first attempt reads:</para> @@ -3785,8 +3922,9 @@ Process finished with exit code 1</screen> </m:math> </inlineequation> (truncating). This indeed yields the desired result for non-negative values. So why does Joe encounter a - division by zero runtime exception when executing <code language="java">longValue - / (2 * (Integer.MAX_VALUE + 1))</code>?</para> + division by zero runtime exception when executing <code + language="java">longValue / (2 * (Integer.MAX_VALUE + + 1))</code>?</para> <para>Unfortunately Joe's implementation is seriously flawed for even two reasons:</para> @@ -3794,13 +3932,14 @@ Process finished with exit code 1</screen> <orderedlist> <listitem> <para>The constant <code language="java" - xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html">Integer</code>.<code language="java" + xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html">Integer</code>.<code + language="java" xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#MAX_VALUE">MAX_VALUE</code> already suggests we will not be able to increase its value while staying as an <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code>. - The expression <code language="java">Integer.MAX_VALUE + 1</code> will be - evaluated using <code language="java" + The expression <code language="java">Integer.MAX_VALUE + + 1</code> will be evaluated using <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> rather than <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> @@ -3813,11 +3952,12 @@ _____________________________________ <para>This is the binary representation of the unintended result <code language="java" - xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html">Integer</code>.<code language="java" + xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html">Integer</code>.<code + language="java" xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#MIN_VALUE">MIN_VALUE</code> - due to an arithmetic overflow. The expression <code language="java">2 * - (Integer.MAX_VALUE + 1)</code> then gives rise to a second - overflow error:</para> + due to an arithmetic overflow. The expression <code + language="java">2 * (Integer.MAX_VALUE + 1)</code> then gives + rise to a second overflow error:</para> <programlisting language="java"> 10000000_00000000_00000000_00000000 + 10000000_00000000_00000000_00000000 @@ -3826,20 +3966,22 @@ _____________________________________ </listitem> </orderedlist> - <para>Both errors combined surprisingly result in a value of - <code language="java">0</code> explaining the <quote>division by zero</quote> - error message. There are two possible solutions:</para> + <para>Both errors combined surprisingly result in a value of <code + language="java">0</code> explaining the <quote>division by + zero</quote> error message. There are two possible + solutions:</para> <glosslist> <glossentry> - <glossterm><code language="java">(int) (longValue / (2L * (Integer.MAX_VALUE + - 1L)))</code></glossterm> + <glossterm><code language="java">(int) (longValue / (2L * + (Integer.MAX_VALUE + 1L)))</code></glossterm> <glossdef> - <para>Introducing <code language="java">2L</code> or <code language="java">1L</code> (one is - sufficient) in favour of simply using 2 and 1 turns both - addition and multiplication into operations involving at - least one <code language="java">long</code> argument. Thus for both + <para>Introducing <code language="java">2L</code> or <code + language="java">1L</code> (one is sufficient) in favour of + simply using 2 and 1 turns both addition and multiplication + into operations involving at least one <code + language="java">long</code> argument. Thus for both operations the <xref linkend="glo_Java"/> runtime will use <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> @@ -3858,23 +4000,25 @@ _____________________________________ </glossentry> <glossentry> - <glossterm><code language="java">(int) (longValue / 2 / (Integer.MAX_VALUE + - 1L))</code></glossterm> + <glossterm><code language="java">(int) (longValue / 2 / + (Integer.MAX_VALUE + 1L))</code></glossterm> <glossdef> <para>Same result as before.</para> <note> - <para>This time the expression starts with <code language="java">longValue - / 2 ...</code> Since the variable <code language="java">longValue</code> - is of type <code language="java" - xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> - the expression <code language="java">longValue / 2</code> will be - evaluated by the <xref linkend="glo_Java"/> runtime using + <para>This time the expression starts with <code + language="java">longValue / 2 ...</code> Since the + variable <code language="java">longValue</code> is of type <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> + the expression <code language="java">longValue / 2</code> + will be evaluated by the <xref linkend="glo_Java"/> + runtime using <code language="java" + xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> arithmetics. The result will subsequently be divided by - <code language="java">Integer.MAX_VALUE + 1L</code> again using <code language="java" + <code language="java">Integer.MAX_VALUE + 1L</code> again + using <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> arithmetic.</para> </note> @@ -3934,8 +4078,9 @@ short sum = a + 7; <co linkends="sd1_co_expressionTypeIntToShort-2" <callout arearefs="sd1_co_expressionTypeIntToShort-2-co" xml:id="sd1_co_expressionTypeIntToShort-2"> - <para>Declaring a short variable <code language="java">sum</code> assigning 4 - + 7.This yields a compile time error:</para> + <para>Declaring a short variable <code + language="java">sum</code> assigning 4 + 7.This yields a + compile time error:</para> <screen>Type mismatch: cannot convert from int to short</screen> </callout> @@ -3950,8 +4095,8 @@ short sum = a + 7; <co linkends="sd1_co_expressionTypeIntToShort-2" </question> <answer> - <para>The expression <code language="java">a + 7</code> contains a variable - <quote>a</quote> of type <code language="java" + <para>The expression <code language="java">a + 7</code> contains a + variable <quote>a</quote> of type <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code> to the plus operator's left and an <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> @@ -3959,19 +4104,21 @@ short sum = a + 7; <co linkends="sd1_co_expressionTypeIntToShort-2" yields an <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> result (the <quote>biggest</quote> type of both operands) which - cannot be assigned to the variable <code language="java">sum</code> of type <code language="java" + cannot be assigned to the variable <code + language="java">sum</code> of type <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code>.</para> <para>The very same argument seems to apply for the expression - <code language="java">4 + 7</code> even better: This time the plus operator acts - on two <code language="java" + <code language="java">4 + 7</code> even better: This time the plus + operator acts on two <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> operands. However the <xref linkend="glo_Java"/> compiler is <quote>clever</quote> enough casting the <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> result 11 to the <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code> - variable <code language="java">sum</code> without any loss of information.</para> + variable <code language="java">sum</code> without any loss of + information.</para> </answer> </qandaentry> </qandadiv> @@ -3988,7 +4135,8 @@ short sum = a + 7; <co linkends="sd1_co_expressionTypeIntToShort-2" <programlisting language="java">System.out.println(8 / 9);</programlisting> <para>The output is <computeroutput>0</computeroutput> rather than - <code language="java">0.888888...</code> . Explain this result.</para> + <code language="java">0.888888...</code> . Explain this + result.</para> </question> <answer> @@ -3999,9 +4147,10 @@ short sum = a + 7; <co linkends="sd1_co_expressionTypeIntToShort-2" <para>According to <uri xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3-300-A-2">https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3-300-A-2</uri> - the floating point value <code language="java">0.88888...</code> will be - <quote>rounded toward zero</quote> yielding an int value of - <code language="java">0</code> rather than <code language="java">1</code>.</para> + the floating point value <code language="java">0.88888...</code> + will be <quote>rounded toward zero</quote> yielding an int value + of <code language="java">0</code> rather than <code + language="java">1</code>.</para> </answer> </qandaentry> </qandadiv> @@ -4054,9 +4203,9 @@ System.out.println("value=" + a); a = a + 1; // Error: Type mismatch: cannot convert from int to byte System.out.println("New value=" + a);</programlisting> - <para>What do you learn about the two operators - <quote><code language="java">+</code></quote> an - <quote><code language="java">a++</code></quote>? Explain this error's + <para>What do you learn about the two operators <quote><code + language="java">+</code></quote> an <quote><code + language="java">a++</code></quote>? Explain this error's cause.</para> </listitem> </orderedlist> @@ -4107,7 +4256,8 @@ System.out.println("New value=" + a);</programlisting> <listitem> <para>The compile time error is due to the definition of the - <quote>+</quote> operator in Java always returning an <code language="java" + <quote>+</quote> operator in Java always returning an <code + language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> rather than a byte. Consider:</para> @@ -4119,8 +4269,8 @@ System.out.println(a + b);</programlisting> xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> value.</para> - <para>If the expression <code language="java">a + b</code> was of data type - <code language="java" + <para>If the expression <code language="java">a + b</code> was + of data type <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-A">byte</code> in <xref linkend="glo_Java"/> an arithmetic overflow as in the subsequent code example would occur:</para> @@ -4132,14 +4282,15 @@ byte sum = (byte) (a + b); System.out.println(sum);</programlisting> <para>The explicit type conversion (a so called <quote>type - cast</quote> or cast for short) forces the 4-byte - <code language="java">int</code> into a one-byte variable <code language="java">sum</code> - thereby loosing its original value and returning -126 - instead.</para> - - <para>Conclusion: <code language="java">a = a + 1</code> and <code language="java">a++</code> - (surprisingly) differ in behaviour when being applied to - non-<code language="java">int</code> variables.</para> + cast</quote> or cast for short) forces the 4-byte <code + language="java">int</code> into a one-byte variable <code + language="java">sum</code> thereby loosing its original value + and returning -126 instead.</para> + + <para>Conclusion: <code language="java">a = a + 1</code> and + <code language="java">a++</code> (surprisingly) differ in + behaviour when being applied to non-<code + language="java">int</code> variables.</para> </listitem> </orderedlist> </answer> @@ -4168,13 +4319,15 @@ System.out.println(2147483647 + 1L);</programlisting> </question> <answer> - <para>The value 2147483647 is actually the largest possible <code language="java" + <para>The value 2147483647 is actually the largest possible <code + language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> value <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html">Integer</classname>.<property xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html#MAX_VALUE">MAX_VALUE</property>. - The plus operator in the first expression <code language="java">2147483647 + - 1</code> acts on two operands of type <code language="java" + The plus operator in the first expression <code + language="java">2147483647 + 1</code> acts on two operands of type + <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code>. Thus the resulting sum will be of type <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> @@ -4199,12 +4352,14 @@ System.out.println(2147483647 + 1L);</programlisting> xlink:href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3-220">http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3-220</uri> for details.</para> - <para>On contrary the plus operator in the expression - <code language="java">2147483647 + 1L</code> acts on an <code language="java" + <para>On contrary the plus operator in the expression <code + language="java">2147483647 + 1L</code> acts on an <code + language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> to its left and a <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> - to its right. The result will thus be of type <code language="java" + to its right. The result will thus be of type <code + language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code>:</para> <screen> 01111111_11111111_11111111_11111111 2147483647 @@ -4249,18 +4404,19 @@ System.out.println(2147483647 + 1L);</programlisting> </question> <answer> - <para>The expression <code language="java">x == y</code> evaluates to - <code language="java">false</code>. This surprising result is due to limited - precision regarding both <code language="java" + <para>The expression <code language="java">x == y</code> evaluates + to <code language="java">false</code>. This surprising result is + due to limited precision regarding both <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">float</code> and <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">double</code> IEEE representations: A given value will be approximated as close as possible.</para> - <para>Adding <code language="java">System.out.println(x - y)</code> yields a value - of -1.1102230246251565E-16 denoting the representational deviation - of <code language="java">x</code> and <code language="java">y</code>.</para> + <para>Adding <code language="java">System.out.println(x - + y)</code> yields a value of -1.1102230246251565E-16 denoting the + representational deviation of <code language="java">x</code> and + <code language="java">y</code>.</para> <para>Comparing <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">float</code> @@ -4512,8 +4668,8 @@ System.out.println("Difference: " + difference);</programlisting><screen>Differe </m:mrow> </m:math> </inlineequation>. Complete the following code to calculate the - result and write it to standard output using - <code language="java">System.out.println(...)</code>:</para> + result and write it to standard output using <code + language="java">System.out.println(...)</code>:</para> <programlisting language="java">public static void main(String[] args) { @@ -4552,9 +4708,9 @@ System.out.println("Difference: " + difference);</programlisting><screen>Differe <glossdef> <para>Instead of immediately using the expression as an - argument to <code language="java">System.out.println(...)</code> we may - assign its value to a variable prior to output - creation:</para> + argument to <code + language="java">System.out.println(...)</code> we may assign + its value to a variable prior to output creation:</para> <programlisting language="java">public static void main(String[] args) { @@ -4629,11 +4785,13 @@ System.out.println("A circle of radius " + radius + " will cover an area of " + </question> <answer> - <para>The solution is straightforward. We add the <code language="java" + <para>The solution is straightforward. We add the <code + language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4">final</code> - modifier to the definition of our variable <code language="java">pi</code>. In - addition we use capital letters <code language="java">PI</code> reflecting the - naming convention for constants:</para> + modifier to the definition of our variable <code + language="java">pi</code>. In addition we use capital letters + <code language="java">PI</code> reflecting the naming convention + for constants:</para> <programlisting language="java" linenumbering="numbered">final double PI = 3.141592653589793; double radius = 2.3; // Computing a circle's area @@ -4953,7 +5111,8 @@ char c = 'A'; // ASCII 65 System.out.println(s + c);</programlisting> <para>Execution results in <computeroutput>66</computeroutput>. - What is the <code language="java">s+c</code> expression's type?</para> + What is the <code language="java">s+c</code> expression's + type?</para> <tip> <para>Try to assign the expression to another variable and @@ -4963,9 +5122,10 @@ System.out.println(s + c);</programlisting> </question> <answer> - <para>Both <code language="java">short</code> and <code language="java">char</code> require two - bytes in <xref linkend="glo_Java"/>. It is thus surprising that a - mixed expression has got neither of both input types:</para> + <para>Both <code language="java">short</code> and <code + language="java">char</code> require two bytes in <xref + linkend="glo_Java"/>. It is thus surprising that a mixed + expression has got neither of both input types:</para> <programlisting language="java">short s = 1; char c = 'A'; @@ -4973,8 +5133,9 @@ short resultShort = s + c; // Incompatible types Required: short found: int char resultChar= s + c; // Incompatible types Required: char found: int</programlisting> <para>According to the compiler's error message the expression - <code language="java">s + c</code> is of type <code language="java">int</code>. Why choosing a - result type being twice the size of both input types?</para> + <code language="java">s + c</code> is of type <code + language="java">int</code>. Why choosing a result type being twice + the size of both input types?</para> <para>The two input types have different ranges:</para> @@ -5037,8 +5198,8 @@ char resultChar= s + c; // Incompatible types Required: char found: int</prog </glossentry> </glosslist> - <para>A promotion to <code language="java">int</code> guarantees correct results - in all circumstances.</para> + <para>A promotion to <code language="java">int</code> guarantees + correct results in all circumstances.</para> </answer> </qandaentry> </qandadiv> @@ -5105,32 +5266,34 @@ System.out.println("e=" + e);</programlisting> (possibly) start wailing about your success rate.</para> <tip> - <para>Both <code language="java">x++</code> (postfix notation) and - <code language="java">++x</code> (infix notation) are expressions themselves - which might even be rewritten as <code language="java">(x++)</code> and - <code language="java">(++x)</code> for the sake of clarity. The difference is - not about operator precedence rules but simply about the values - of these expressions when being assigned to other - variables.</para> + <para>Both <code language="java">x++</code> (postfix notation) + and <code language="java">++x</code> (infix notation) are + expressions themselves which might even be rewritten as <code + language="java">(x++)</code> and <code + language="java">(++x)</code> for the sake of clarity. The + difference is not about operator precedence rules but simply + about the values of these expressions when being assigned to + other variables.</para> </tip> </question> <answer> <para>As inferred by the hint the biggest problem is about - understanding postfix and infix notation of the operators - <code language="java">++</code> and <code language="java">--</code>. A corresponding expression - evaluates to:</para> + understanding postfix and infix notation of the operators <code + language="java">++</code> and <code language="java">--</code>. A + corresponding expression evaluates to:</para> <itemizedlist> <listitem> - <para><code language="java">a = x++</code> yields a ==x: The value of x - <emphasis role="bold">before</emphasis> incrementing - it.</para> + <para><code language="java">a = x++</code> yields a ==x: The + value of x <emphasis role="bold">before</emphasis> + incrementing it.</para> </listitem> <listitem> - <para><code language="java">a = ++x</code> yields a ==(x + 1): The value of x - <emphasis role="bold">after</emphasis> incrementing it.</para> + <para><code language="java">a = ++x</code> yields a ==(x + 1): + The value of x <emphasis role="bold">after</emphasis> + incrementing it.</para> </listitem> </itemizedlist> @@ -5182,9 +5345,9 @@ System.out.println("a = " + a + ", b = " + b + ", c = " + c);</programlisting> <screen>a = 7, b = 7, c = 1</screen> <para>Decompose this cryptic assignment into a series of multiple - elementary ones like e.g. <code language="java">a++</code> by possibly introducing - one or more helper variables. Thus your code shall become longer - but better to understand.</para> + elementary ones like e.g. <code language="java">a++</code> by + possibly introducing one or more helper variables. Thus your code + shall become longer but better to understand.</para> <tip> <para>Read about evaluation of expressions and <link @@ -5218,17 +5381,19 @@ System.out.println("a = " + a + ", b = " + b + ", c = " + c);</programlisting> <listitem> <para><code language="java">++b % c--</code></para> - <para>This one deserves further attention: The <code language="java">%</code> - operator will act on <code language="java">b</code>'s value <emphasis + <para>This one deserves further attention: The <code + language="java">%</code> operator will act on <code + language="java">b</code>'s value <emphasis role="bold">after</emphasis> incrementing (infix notation) and - <code language="java">c</code>'s value <emphasis role="bold">before</emphasis> - decrementing (postfix notation).</para> + <code language="java">c</code>'s value <emphasis + role="bold">before</emphasis> decrementing (postfix + notation).</para> </listitem> </orderedlist> - <para>We introduce a helper variable <code language="java">sum</code> for - decomposing our code into the above described four - subexpressions:</para> + <para>We introduce a helper variable <code + language="java">sum</code> for decomposing our code into the above + described four subexpressions:</para> <programlisting language="java">int a = 3, b = 6, c = 2;