From 59d1d760bdd3889112a6aba6c2352e7a460f6b6e Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Tue, 17 Sep 2019 22:03:24 +0300 Subject: [PATCH] Operator ++, pre/post incremnt, related execises --- Doc/Sd1/languageFundamentals.xml | 400 ++++++++++++++++++++++++------- 1 file changed, 316 insertions(+), 84 deletions(-) diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml index e88e2f01f..dfc3ad6a3 100644 --- a/Doc/Sd1/languageFundamentals.xml +++ b/Doc/Sd1/languageFundamentals.xml @@ -2910,37 +2910,6 @@ System.out.println(intensity);</programlisting> </qandadiv> </qandaset> - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_literalProblem"> - <title>Literal problem</title> - - <qandadiv> - <qandaentry> - <question> - <para>Consider:</para> - - <programlisting language="java">System.out.println(048);</programlisting> - - <para>This yields a compile time error <quote>Integer number too - large</quote>. On contrary the following code will compile and run - perfectly well:</para> - - <programlisting language="java">System.out.println(047);</programlisting> - - <para>Explain the underlying problem.</para> - </question> - - <answer> - <para>The literal <quote>048</quote> denotes <link - xlink:href="https://en.wikipedia.org/wiki/Octal">octal - representation</link>. Digits in octal representation range from 0 - to 7. So <quote>047</quote> is fine but <quote>048</quote> is - <link linkend="sd1_qanda_illegalOctalLiteral">again</link> - invalid.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - <qandaset defaultlabel="qanda" xml:id="sd1QandaBinaryIntLiteral"> <title>Binary literals</title> @@ -3715,6 +3684,187 @@ Expected value: 9223372036854775807</screen> </imageobject> </mediaobject> </figure> + </section> + + <section xml:id="sectConversions"> + <title>Conversions</title> + + <figure xml:id="fig_WideningByte2Short"> + <title>Widening from <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> + literal to <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></title> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/byte2short.svg"/> + </imageobject> + </mediaobject> + </figure> + + <figure xml:id="fig_NarrowingInt2Char"> + <title>Narrowing from <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> + literal to <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> + variable</title> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/int2char.svg"/> + </imageobject> + </mediaobject> + </figure> + + <figure xml:id="sd1_fig_wideningLadder"> + <title>A widening «ladder»</title> + + <programlisting language="java">byte byteVariable = 42; // Narrowing: int literal to byte +short shortVariable = byteVariable; // Widening +int intVariable = shortVariable; // Widening +float floatVariable = intVariable; // Widening +double doubleVariable = floatVariable; // Widening</programlisting> + </figure> + + <figure xml:id="sd1_fig_narrowingLadder"> + <title>A narrowing «ladder»</title> + + <programlisting language="java">double doubleVariable = 14.23; +float floatVariable = (float) doubleVariable; // Narrowing +int intVariable = (int) floatVariable; // Narrowing +short shortVariable = (short) intVariable; // Narrowing +byte byteVariable = (byte) shortVariable; // Narrowing</programlisting> + </figure> + + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_intConverChar"> + <title><code language="java">int</code> and <code + language="java">char</code></title> + + <qandadiv> + <qandaentry> + <question> + <para>Explain the following output:</para> + + <informaltable border="1"> + <tr> + <th>Code</th> + + <th>Output</th> + </tr> + + <tr> + <td valign="top"><programlisting language="java">char a = 0; +a -= 1; +int i = a; +System.out.println(i);</programlisting></td> + + <td valign="top"><screen>65535</screen></td> + </tr> + </informaltable> + + <para>Why do we see a result of 65535 rather than -1?</para> + + <tip> + <para>Read about the a <code language="java">char</code>'s value + range.</para> + </tip> + </question> + + <answer> + <para>In contrast to <code language="java">byte</code>, <code + language="java">short</code> and <code language="java">int</code> + a two byte <code language="java">char</code> represents no + negative but only positive values and zero:</para> + + <informaltable border="1"> + <tr> + <th>decimal</th> + + <th>binary</th> + </tr> + + <tr> + <td valign="top">0</td> + + <td valign="top"><code language="java">0000 0000 0000 + 0000</code></td> + </tr> + + <tr> + <td valign="top">1</td> + + <td valign="top"><code language="java">0000 0000 0000 + 0001</code></td> + </tr> + + <tr> + <td valign="top">2</td> + + <td valign="top"><code language="java">0000 0000 0000 + 0010</code></td> + </tr> + + <tr> + <td align="center" colspan="2">...</td> + </tr> + + <tr> + <td valign="top">65535 (<inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> + + <m:mi>16</m:mi> + </m:msup> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation>)</td> + + <td valign="top"><code language="java">1111 1111 1111 + 1111</code></td> + </tr> + </informaltable> + + <para>Like with other integer types the above table behaves in a + cyclic way with respect to additions at its top and and + subtractions at its bottom:</para> + + <informaltable border="1"> + <tr> + <th>Code</th> + + <th>Output</th> + </tr> + + <tr> + <td valign="top"><programlisting language="java">char a = 65535; +a += 1; +int i = a; +System.out.println(i);</programlisting></td> + + <td valign="top"><screen>0</screen></td> + </tr> + </informaltable> + + <para>On machine level this may be conceived as an (incomplete) + subtract operation:</para> + + <programlisting language="none"> 0000 0000 0000 0000 +-0000 0000 0000 0001 +-------------------- + 1111 1111 1111 1111 + + +</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1_qanda_floatDoubleLiteral"> <title><code>float</code> vs. <code>double</code></title> @@ -3915,36 +4065,6 @@ System.out.println( 3.14d );</programlisting></td> </qandaentry> </qandadiv> </qandaset> - </section> - - <section xml:id="sectConversions"> - <title>Conversions</title> - - <figure xml:id="fig_WideningByte2Short"> - <title>Widening from <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> - to <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></title> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/byte2short.svg"/> - </imageobject> - </mediaobject> - </figure> - - <figure xml:id="fig_NarrowingInt2Char"> - <title>Narrowing from <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 <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></title> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/int2char.svg"/> - </imageobject> - </mediaobject> - </figure> <qandaset defaultlabel="qanda" xml:id="qandaNarrowingInt2Char"> <title><code language="java" @@ -4925,8 +5045,8 @@ New value=-128</screen> </listitem> <listitem> - <para>Moreover the following yields a compile time - error:</para> + <para>Moreover the following logically equivalent code yields + a compile time error:</para> <programlisting language="java">byte a = 127; System.out.println("value=" + a); @@ -4986,36 +5106,42 @@ 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" + binary <quote>+</quote> operator: In Java it'll 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> + rather than a byte irrespective of it's operands' types. + Consider:</para> <programlisting language="java">byte a = 120, b = 10; System.out.println(a + b);</programlisting> - <para>This yields the expected output of 130 and corresponds - to an <code language="java" + <para>This yields the expected output of 130 corresponding 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> value.</para> <para>If the expression <code language="java">a + b</code> was - of data type <code language="java" + of 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> + an arithmetic overflow as in the subsequent code example would + occur:</para> <programlisting language="java">byte a = 120, b = 10; -byte sum = (byte) (a + b); - -System.out.println(sum);</programlisting> +byte sum = (byte) (a + b); // yields -126</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> + language="java">int</code> representing 130 into a one-byte + variable <code language="java">sum</code> thereby loosing its + original value and assigning -126 instead:</para> + + <screen> 120 00000000 00000000 00000000 01111000 ++ 10 + 00000000 00000000 00000000 00001010 +---- ------------------------------------- + 130 <emphasis role="red">00000000 00000000 00000000</emphasis> 10000010 + +-126 cast int to byte: -----> 10000010</screen> <para>Conclusion: <code language="java">a = a + 1</code> and <code language="java">a++</code> (surprisingly) differ in @@ -5341,7 +5467,7 @@ System.out.println("Difference: " + difference);</programlisting><screen>Differe <tr> <td><code language="java">byte</code></td> - <td><code language="java">short</code></td> + <td><code language="java">byte</code></td> <td><code language="java">int</code></td> </tr> @@ -5373,11 +5499,24 @@ System.out.println("Difference: " + difference);</programlisting><screen>Differe </figure> <para xml:id="sd1_explainNoByteByteOperator">The careful reader may - stumble upon the absence of e.g. a «+» operator turning two byte values - <code language="java">a</code> and <code language="java">b</code> into an - expression <code language="java">a + b</code> of type <code - language="java">byte</code> rather than <code language="java">int</code>. - This is due to a Java Virtual Machine <link + stumble upon the absence of e.g. a binary «+» operator turning two byte + values <code language="java">a</code> and <code language="java">b</code> + into an expression <code language="java">a + b</code> of type <code + language="java">byte</code> rather than <code + language="java">int</code>:</para> + + <figure xml:id="sd1_fig_noBinaryPlusByteByte"> + <title>No binary + operator yielding <code + language="java">byte</code></title> + + <programlisting language="java">byte a = 1, b = 2; + +<emphasis role="red">byte sum = a + b; // Error: Incompatible types. + // Required: byte + // Found: int</emphasis></programlisting> + </figure> + + <para>This is due to a Java Virtual Machine <link xlink:href="https://docs.oracle.com/javase/specs/jvms/se10/html/jvms-2.html#jvms-2.11.1-120">design decision</link> leading to a <link xlink:href="https://docs.oracle.com/javase/specs/jvms/se10/html/jvms-2.html#jvms-2.11.1-320">limited @@ -6144,11 +6283,104 @@ char resultChar= s + c; // Incompatible types Required: char found: int</prog </qandadiv> </qandaset> + <figure xml:id="sd1_fig_OperatorPlusPlus"> + <title>Unary <code language="java">++</code> operator</title> + + <para>Increment variable by 1:</para> + + <informaltable border="0"> + <tr> + <td valign="top"><programlisting language="none">int a = 4; +System.out.println("Before:" + a); +<emphasis role="red">a = a + 1</emphasis>; +System.out.println("After:" + a);</programlisting></td> + + <td valign="top"><programlisting language="none">int a = 4; +System.out.println("Before:" + a); +<emphasis role="red">a++</emphasis>; +System.out.println("After:" + a);</programlisting></td> + </tr> + + <tr> + <th colspan="2"><screen>Before:4 +After:5</screen></th> + </tr> + </informaltable> + </figure> + + <figure xml:id="sd1_fig_OperatorPlusPlusPostfixPrefix"> + <title>Prefix and postfix notation</title> + + <informaltable border="1"> + <colgroup width="7%"/> + + <colgroup width="44%"/> + + <colgroup width="7%"/> + + <colgroup width="42%"/> + + <tr> + <th colspan="2">pre-increment</th> + + <th colspan="2">post-increment</th> + </tr> + + <tr> + <td colspan="2" valign="top"><programlisting language="none">int a = 4; +int b = <emphasis role="red">++a</emphasis>; +System.out.println(b);</programlisting></td> + + <td colspan="2" valign="top"><programlisting language="none">int a = 4; +int b = <emphasis role="red">a++</emphasis>; +System.out.println(b);</programlisting></td> + </tr> + + <tr> + <td>Output: </td> + + <td valign="top"><screen>5</screen></td> + + <td>Output:</td> + + <td valign="top"><screen>4</screen></td> + </tr> + </informaltable> + </figure> + + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_threeQaysExpression"> + <title>Three ways expressing the same</title> + + <qandadiv> + <qandaentry> + <question> + <para>Write the expression <code language="java">a = a - 1</code> + in two alternate ways.</para> + + <tip> + <para>Use two different operators.</para> + </tip> + </question> + + <answer> + <itemizedlist> + <listitem> + <para><code language="java">a -= 1</code></para> + </listitem> + + <listitem> + <para><code language="java">a--</code></para> + </listitem> + </itemizedlist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + <figure xml:id="sd1_fig_OperatorExamples"> <title>Operator examples</title> <programlisting language="java">//Integer -i++; i = i + 1; // Increment by one i--; i = i - 1; // Decrement by one i += b; i = i + b; // Raise by b's value i %= 3; i = i % 3; // Modulus of 3 -- GitLab