diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml index 44219c2a38df408d635901c05b6bd795b47e73a0..c582ca9a55719e4009461b6a78f63229cf4e36ca 100644 --- a/Doc/Sd1/languageFundamentals.xml +++ b/Doc/Sd1/languageFundamentals.xml @@ -184,7 +184,7 @@ <td>no</td> - <td><code>for</code> is a Java keyword.</td> + <td><quote>for</quote> is a Java keyword.</td> </tr> <tr> @@ -192,7 +192,7 @@ <td>yes</td> - <td/> + <td>-</td> </tr> <tr> @@ -200,7 +200,7 @@ <td>yes</td> - <td/> + <td>-</td> </tr> <tr> @@ -208,8 +208,8 @@ <td>no</td> - <td>Operators like <quote>-</quote> must not appear in - variable names.</td> + <td>Operators like <quote>-</quote> or <quote>+</quote> must + not appear in variable names.</td> </tr> <tr> @@ -234,7 +234,7 @@ <td>yes</td> - <td><code>println</code> is part of + <td><code>println</code> is a method of <code>System.out.println(...)</code> but is no <xref linkend="glo_Java"/> keyword.</td> </tr> @@ -253,8 +253,9 @@ <td>yes</td> - <td>Best practices: Discouraged variable name, non-constant - variables should start with lowercase letters.</td> + <td>Best practices: Legal but discouraged variable name: + Non-constant variables should start with lowercase + letters.</td> </tr> <tr> @@ -287,7 +288,8 @@ <td>yes</td> - <td>Best practices: Should be reserved for system code.</td> + <td>Best practices: Using the <quote>$</quote> sign should be + reserved for system code.</td> </tr> </informaltable> </answer> @@ -299,148 +301,11 @@ <section xml:id="sw1IntegerLiterals"> <title>Integer value literals</title> - <qandaset defaultlabel="qanda" xml:id="sw1QandaMaxMinInt"> - <title>An <code>int</code>'s minimum and maximum value</title> - - <qandadiv> - <qandaentry> - <question> - <para>In this exercise we look at an <code>int</code>'s the - largest and smallest possible value.</para> - - <para>A <xref linkend="glo_Java"/> <code - xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">int</code> - is internally being represented by 4 <link - linkend="glo_byte">bytes</link>. <inlineequation> - <m:math display="inline"> - <m:msub> - <m:mi>00000000000000000000000000000101</m:mi> - - <m:mi>2</m:mi> - </m:msub> - </m:math> - </inlineequation> for example represents the decimal value - 5.</para> - - <para>In order to represent negative values as well <xref - linkend="glo_Java"/> uses <link - xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representation. We provide some values:</para> - - <table border="1" xml:id="sw1Table4ByteIntegerRepresentation"> - <caption>4 Byte <link - xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representation of <code>int</code> - values.</caption> - - <tr> - <th>Two complement representation</th> - - <th>Decimal representation</th> - </tr> - - <tr> - <td><code>00000000000000000000000000000000</code></td> - - <td>0</td> - </tr> - - <tr> - <td><code>01111111111111111111111111111111</code></td> - - <td><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> - - <m:mrow> - <m:mi>16</m:mi> - - <m:mo>-</m:mo> - - <m:mi>1</m:mi> - </m:mrow> - </m:msup> - - <m:mo>-</m:mo> - - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation> (Maximum)</td> - </tr> - - <tr> - <td><code>10000000000000000000000000000000</code></td> - - <td><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>-</m:mo> - - <m:msup> - <m:mi>2</m:mi> - - <m:mrow> - <m:mi>16</m:mi> - - <m:mo>-</m:mo> - - <m:mi>1</m:mi> - </m:mrow> - </m:msup> - </m:mrow> - </m:math> - </inlineequation> (Minimum)</td> - </tr> - - <tr> - <td><code>11111111111111111111111111111111</code></td> - - <td>-1</td> - </tr> - </table> - - <para>Use <link - xlink:href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1"><code>int</code> - literals</link> in <link - xlink:href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-BinaryNumeral">binary - representation</link> like e.g. <code>0B1100</code> in section - <quote - xlink:href="http://proquest.safaribooksonline.com/9780992133047/toc6_html_2">Literals</quote> - in order to write an <code>int</code>'s minimum and maximum - possible value to standard output.</para> - - <programlisting language="noneJava"> public static void main(String[] args) { - - int minumum = ... , //TODO: provide values by - maximum = ...; // binary int literals - - System.out.println("Minimum:" + minumum); - System.out.println("Maximum:" + maximum); - }</programlisting> - </question> - - <answer> - <para>We insert <link - xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representations of minimum and maximum int - values according to <xref - linkend="sw1Table4ByteIntegerRepresentation"/>.</para> + <section xml:id="sd1BinaryLiteral"> + <title>Binary literals</title> - <programlisting language="noneJava">public static void main(String[] args) { - - int minumum = 0B10000000_00000000_00000000_00000000, - maximum = 0B01111111_11111111_11111111_11111111; - - System.out.println("Minimum int value:" + minumum); - System.out.println("Maximum int value:" + maximum); - }</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + <para>TODO:</para> + </section> <section xml:id="sw1SectHexadecimalLiterals"> <title>Hexadecimal literals</title> @@ -546,8 +411,8 @@ xlink:href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.1">specification</link>.</para> <para>The programmer tries to adopt the <link - linkend="sw1QandaMaxMinInt">related int example</link> and - adopts it accordingly:</para> + linkend="sw1QandaMaxMinInt">related int example</link> + accordingly:</para> <programlisting language="noneJava">short minumum = 0B10000000_00000000, maximum = 0B01111111_11111111; @@ -630,20 +495,264 @@ System.out.println("Maximum short value:" + maximum); an integer value which is compatible with a short and is thus assignable.</para> - <para>On the other hand the <code>int</code> literal - <code>0B10000000_00000000</code> evaluates to +32768 and can - thus not be assigned to a short at all. Not to mention its non- - <link + <para>On the other hand the binary value + <code>10000000_00000000</code> evaluates to <inlineequation> + <m:math display="inline"> + <m:msup> + <m:mn>2</m:mn> + + <m:mi>15</m:mi> + </m:msup> + </m:math> + </inlineequation> (equal to 32768) and thus cannot be + <emphasis>generally</emphasis> assigned to a two-byte short + variable without loosing data.</para> + + <para>Even worse: The expression + <code>0B10000000_00000000</code> is actually a four byte + <code>int</code> literal using <link xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representation. In order to avoid this error - we need an explicit cast:</para> + complement</link> representation. Thus its value is + <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> + + <m:msup> + <m:mn>2</m:mn> + + <m:mi>15</m:mi> + </m:msup> + </m:mrow> + </m:math> + </inlineequation> (equal to -32768).</para> + + <para>In order to avoid this error we need an explicit type + conversion (a so called cast):</para> <programlisting language="noneJava">short minumum = (short) 0B10000000_00000000;</programlisting> - <para>Since a short ranges from -32768 to 32767 we may as well - use a unary minus sign instead:</para> + <para>Since a <code>short</code> variable's range is + <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>15</m:mi> + </m:msup> + </m:mrow> + + <m:mo>,</m:mo> + + <m:mrow> + <m:msup> + <m:mi>2</m:mi> + + <m:mi>15</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> we may as well use a unary minus sign + instead:</para> <programlisting language="noneJava">short minumum = -0B10000000_00000000;</programlisting> + + <para>Both variants works syntactically but this is likely not + our programmer's intended value: The error in the first place is + probably the intention to assign a 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>to a variable of type + <code>short</code>.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sd1SectIntMinMax"> + <title>An <code>int</code>'s minimum and maximum value</title> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaMaxMinInt"> + <qandadiv> + <qandaentry> + <question> + <para>In this exercise we look at an <code>int</code>'s the + largest and smallest possible value.</para> + + <para>A <xref linkend="glo_Java"/> <code + xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">int</code> + is internally being represented by 4 <link + linkend="glo_byte">bytes</link>. <inlineequation> + <m:math display="inline"> + <m:msub> + <m:mi>00000000000000000000000000000101</m:mi> + + <m:mi>2</m:mi> + </m:msub> + </m:math> + </inlineequation> for example represents the decimal value + 5.</para> + + <para>In order to represent negative values as well <xref + linkend="glo_Java"/> uses <link + xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's + complement</link> representation. We provide some values:</para> + + <table border="1" xml:id="sw1Table4ByteIntegerRepresentation"> + <caption>4 Byte <link + xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's + complement</link> representation of <code>int</code> + values.</caption> + + <tr> + <th>Two complement representation</th> + + <th>Decimal representation</th> + </tr> + + <tr> + <td><code>00000000000000000000000000000000</code></td> + + <td>0</td> + </tr> + + <tr> + <td><code>01111111111111111111111111111111</code></td> + + <td><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> + + <m:mrow> + <m:mi>16</m:mi> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:msup> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation> (Maximum)</td> + </tr> + + <tr> + <td><code>10000000000000000000000000000000</code></td> + + <td><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> + + <m:msup> + <m:mi>2</m:mi> + + <m:mrow> + <m:mi>16</m:mi> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:msup> + </m:mrow> + </m:math> + </inlineequation> (Minimum)</td> + </tr> + + <tr> + <td><code>11111111111111111111111111111111</code></td> + + <td>-1</td> + </tr> + </table> + + <para>Use <link + xlink:href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1"><code>int</code> + literals</link> in <link + xlink:href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-BinaryNumeral">binary + representation</link> like e.g. <code>0B1100</code> in section + <quote + xlink:href="http://proquest.safaribooksonline.com/9780992133047/toc6_html_2">Literals</quote> + in order to write an <code>int</code>'s minimum and maximum + possible value to standard output.</para> + + <programlisting language="noneJava"> public static void main(String[] args) { + + int minumum = ... , //TODO: provide values by + maximum = ...; // binary int literals + + System.out.println("Minimum:" + minumum); + System.out.println("Maximum:" + maximum); + }</programlisting> + </question> + + <answer> + <para>We insert <link + xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's + complement</link> representations of minimum and maximum int + values according to <xref + linkend="sw1Table4ByteIntegerRepresentation"/>.</para> + + <programlisting language="noneJava">public static void main(String[] args) { + + int minumum = 0B10000000_00000000_00000000_00000000, + maximum = 0B01111111_11111111_11111111_11111111; + + System.out.println("Minimum int value:" + minumum); + System.out.println("Maximum int value:" + maximum); + }</programlisting> + + <para>BTW: The <xref linkend="glo_JDK"/> does provide maximum + value, minimum value and related information for + <code>char</code>, <code>byte</code>, <code>short</code> and + <code>int</code> data types int its related <classname + xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html">Character</classname>, + <classname + xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html">Byte</classname>, + <classname + xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Short.html">Short</classname> + and <classname + xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html">Integer</classname> + classes. You may want to execute:</para> + + <programlisting language="none"> System.out.println("int minimum:" + Integer.MIN_VALUE); + System.out.println("int minimum:" + Integer.MAX_VALUE); + + System.out.println("int bytes:" + Integer.BYTES); + System.out.println("int size:" + Integer.SIZE); +</programlisting> </answer> </qandaentry> </qandadiv>