diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml index 00c3b118b2efea48e009a6cdc62b58d475247748..086d55408467d1d449119b5320b3653fb765d94b 100644 --- a/Doc/Sd1/languageFundamentals.xml +++ b/Doc/Sd1/languageFundamentals.xml @@ -1,25 +1,14 @@ <?xml version="1.0" encoding="UTF-8"?> -<chapter annotations="slide" version="5.1" xml:id="sw1LanguageFundamental" - xmlns="http://docbook.org/ns/docbook" - xmlns:xlink="http://www.w3.org/1999/xlink" - xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes" - xmlns:xi="http://www.w3.org/2001/XInclude" - xmlns:trans="http://docbook.org/ns/transclusion" - xmlns:svg="http://www.w3.org/2000/svg" - xmlns:m="http://www.w3.org/1998/Math/MathML" - xmlns:html="http://www.w3.org/1999/xhtml" - xmlns:db="http://docbook.org/ns/docbook"> +<chapter annotations="slide" version="5.1" xml:id="sw1LanguageFundamental" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:trans="http://docbook.org/ns/transclusion" xmlns:svg="http://www.w3.org/2000/svg" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:db="http://docbook.org/ns/docbook"> <title>Language Fundamentals</title> <section xml:id="sd1_sect_langFundamental_intro"> <title>Introduction</title> <section xml:id="sd1_fig_asciiAndUnicode"> - <title>Integer, <xref linkend="glo_ASCII"/> and <xref - linkend="glo_unicode"/></title> + <title>Integer, <xref linkend="glo_ASCII"/> and <xref linkend="glo_unicode"/></title> - <para>We will deal with computation machines. - Having just mechanical devices at hand this still works:</para> + <para>We will deal with computation machines. Having just mechanical devices at hand this still works:</para> <figure xml:id="sd1_fig_cashier"> <title>Cash register</title> @@ -51,9 +40,7 @@ </mediaobject> </figure> - <para>So far all machines being described are based on non-semiconductor - technologies. Inventing the transistor in the fifties gave rise to a - rapid development of microprocessor chips:</para> + <para>So far all machines being described are based on non-semiconductor technologies. Inventing the transistor in the fifties gave rise to a rapid development of microprocessor chips:</para> <figure xml:id="sd1_fig_microprocessors"> <title>Microprocessor ICs</title> @@ -65,9 +52,7 @@ </mediaobject> </figure> - <para>These sample devices differ heavily with respect to addressable - memory, data size, supported arithmetic operations / speed and other - features. We take a closer look to Zilog's Z80 processor:</para> + <para>These sample devices differ heavily with respect to addressable memory, data size, supported arithmetic operations / speed and other features. We take a closer look to Zilog's Z80 processor:</para> <figure xml:id="sd1_fig_z80"> <title>Zilog Z80 CPU</title> @@ -79,8 +64,7 @@ </mediaobject> </figure> - <para>Following technological advances processors have been categorized - by the length the so called address- and data-bus:</para> + <para>Following technological advances processors have been categorized by the length the so called address- and data-bus:</para> <figure xml:id="sd1_fig_adressDataBus"> <title>Address and data bus sizes</title> @@ -148,9 +132,7 @@ </informaltable> </figure> - <para>We remind the reader to the binary representation of signed - integer values. Details will be discussed in your math lectures. Our - first example features three bit 2-complement:</para> + <para>We remind the reader to the binary representation of signed integer values. Details will be discussed in your math lectures. Our first example features three bit 2-complement:</para> <figure xml:id="sd1_fig_signed3bitIntDual"> <title>Signed 3 bit integer representation</title> @@ -162,8 +144,7 @@ </mediaobject> </figure> - <para>Signed <code>byte</code> values are being represented - accordingly:</para> + <para>Signed <code>byte</code> values are being represented accordingly:</para> <figure xml:id="sd1_fig_signedIntRepresentBinary"> <title>Signed 8 bit integer binary representation</title> @@ -175,8 +156,7 @@ </mediaobject> </figure> - <para>Regarding language characters we start with one of the oldest and - widespread character encoding schemes:</para> + <para>Regarding language characters we start with one of the oldest and widespread character encoding schemes:</para> <figure xml:id="sd1_fig_7bitASCII"> <title>7-bit <xref linkend="glo_ASCII"/></title> @@ -188,10 +168,7 @@ </mediaobject> </figure> - <para><xref linkend="glo_ASCII"/> by design is limited to US characters - and does not include characters from non-English languages. Since <xref - linkend="glo_ASCII"/> only uses seven bits we may a parity bit to form - one byte per character:</para> + <para><xref linkend="glo_ASCII"/> by design is limited to US characters and does not include characters from non-English languages. Since <xref linkend="glo_ASCII"/> only uses seven bits we may a parity bit to form one byte per character:</para> <figure xml:id="sd1_fig_7bitASCII_andParity"> <title>7-bit <xref linkend="glo_ASCII"/> with even parity bit</title> @@ -203,16 +180,10 @@ </mediaobject> </figure> - <para>A byte's parity bit may instead be used for encoding non- <xref - linkend="glo_ASCII"/> supplementary characters. One such example is the - <link xlink:href="https://en.wikipedia.org/wiki/ISO/IEC_8859-1">ISO - 8859-1 (ISO Latin 1)</link> standard representing western European - character sets:</para> + <para>A byte's parity bit may instead be used for encoding non- <xref linkend="glo_ASCII"/> supplementary characters. One such example is the <link xlink:href="https://en.wikipedia.org/wiki/ISO/IEC_8859-1">ISO 8859-1 (ISO Latin 1)</link> standard representing western European character sets:</para> <figure xml:id="sd1_fig_iso8859_1"> - <title><link - xlink:href="https://en.wikipedia.org/wiki/ISO/IEC_8859-1">ISO Latin - 1</link> encoding</title> + <title><link xlink:href="https://en.wikipedia.org/wiki/ISO/IEC_8859-1">ISO Latin 1</link> encoding</title> <mediaobject> <imageobject> @@ -221,13 +192,10 @@ </mediaobject> </figure> - <para>Supporting additional languages comes at a price: We have to - increase the number of bytes representing a single character:</para> + <para>Supporting additional languages comes at a price: We have to increase the number of bytes representing a single character:</para> <figure xml:id="sd1_fig_unicodeSample"> - <title>Some <xref linkend="glo_unicode"/> <link - xlink:href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</link> - samples</title> + <title>Some <xref linkend="glo_unicode"/> <link xlink:href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</link> samples</title> <mediaobject> <imageobject> @@ -236,19 +204,13 @@ </mediaobject> </figure> - <para>Notice the representation's differing byte count: <link - xlink:href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</link> <xref - linkend="glo_unicode"/> encoding allows for one-, two-, three- and four- - byte encodings. See <quote - xlink:href="https://betterexplained.com/articles/unicode">Unicode and - You</quote> for further details.</para> + <para>Notice the representation's differing byte count: <link xlink:href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</link> <xref linkend="glo_unicode"/> encoding allows for one-, two-, three- and four- byte encodings. See <quote xlink:href="https://betterexplained.com/articles/unicode">Unicode and You</quote> for further details.</para> </section> <section xml:id="sd1_sect_variables"> <title>Variables</title> - <para>Variables are convenient handles for accessing memory. We don't - need to mess with memory addresses:</para> + <para>Variables are convenient handles for accessing memory. We don't need to mess with memory addresses:</para> <figure xml:id="sd1_fig_varMemoryHandle"> <title>Handle to memory</title> @@ -260,8 +222,7 @@ </mediaobject> </figure> - <para>Declaring a variable requires a type name like <code>double</code> - and an identifier:</para> + <para>Declaring a variable requires a type name like <code>double</code> and an identifier:</para> <figure xml:id="sd1_fig_varDeclare"> <title>Variable declaration</title> @@ -269,8 +230,7 @@ <programlisting language="java">double pi;</programlisting> </figure> - <para>We may assign values to variables or build expressions like - <code>pi * 2.0 * 2.0</code> :</para> + <para>We may assign values to variables or build expressions like <code>pi * 2.0 * 2.0</code> :</para> <figure xml:id="sd1_fig_varDeclareAndUse"> <title>Declare and use</title> @@ -338,13 +298,11 @@ int c;</programlisting> <itemizedlist> <listitem> - <para>Start with a small letter like <code>africa</code> rather - than <code>Africa</code>.</para> + <para>Start with a small letter like <code>africa</code> rather than <code>Africa</code>.</para> </listitem> <listitem> - <para>Use <quote>camel case</quote> e.g. - <code>myFirstCode</code>.</para> + <para>Use <quote>camel case</quote> e.g. <code>myFirstCode</code>.</para> <mediaobject> <imageobject> @@ -457,6 +415,272 @@ int Count = 44; System.out.println(count + ":" + Count); // This yields "32:44"</programlisting> </figure> + <qandaset defaultlabel="qanda" xml:id="sw1QandaLegalVarNames"> + <title>Legal variable names</title> + + <qandadiv> + <qandaentry> + <question> + <para>Which of the following names are legal variable names? Complete the following table and explain your decision with respect to the <quote xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc4_html_3">variables</quote> section.</para> + + <informaltable border="1"> + <colgroup width="19%"/> + + <colgroup width="10%"/> + + <colgroup width="71%"/> + + <tr> + <th>Identifier</th> + + <th>is legal? (yes / no)</th> + + <th>Explanation / remark</th> + </tr> + + <tr> + <td><code>for</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>sum_of_data</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>sumOfData</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>first-name</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>ABC</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>42isThesolution</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>println</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>B4</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>AnnualSalary</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>"hello"</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>_average </code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>ανδÏος</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>$sum </code></td> + + <td/> + + <td/> + </tr> + </informaltable> + + <tip> + <para>You may want to prepare a simple <xref linkend="glo_Java"/> program testing the above names.</para> + </tip> + </question> + + <answer> + <para>We may write a small <xref linkend="glo_Java"/> program:</para> + + <programlisting language="java">public static void main(String[] args) { + int 42isThesolution = 6; // <emphasis role="bold">Syntax error on token "42", delete this token</emphasis> +}</programlisting> + + <para>Unfortunately the error explanation is not too helpful with respect to <xref linkend="glo_Java"/> beginners. It does not immediately point towards an illegal identifier problem. One indication of a sound compiler is its ability to provide meaningful, self explanatory error messages.</para> + + <informaltable border="1"> + <colgroup width="19%"/> + + <colgroup width="10%"/> + + <colgroup width="71%"/> + + <tr> + <th>Identifier</th> + + <th>is legal? (yes / no)</th> + + <th>Explanation / remark</th> + </tr> + + <tr> + <td><code>for</code></td> + + <td>no</td> + + <td><quote>for</quote> is a Java keyword.</td> + </tr> + + <tr> + <td><code>sum_of_data</code></td> + + <td>yes</td> + + <td>-</td> + </tr> + + <tr> + <td><code>sumOfData</code></td> + + <td>yes</td> + + <td>-</td> + </tr> + + <tr> + <td><code>first-name</code></td> + + <td>no</td> + + <td>Operators like <quote>-</quote> or <quote>+</quote> must not appear in variable names.</td> + </tr> + + <tr> + <td><code>ABC</code></td> + + <td>yes</td> + + <td>Best practices: Discouraged variable name, non-constant variables should start with lowercase letters.</td> + </tr> + + <tr> + <td><code>42isThesolution</code></td> + + <td>no</td> + + <td>Identifiers must not start with a number.</td> + </tr> + + <tr> + <td><code>println</code></td> + + <td>yes</td> + + <td><code>println</code> is a method of <code>System.out.println(...)</code> but is no <xref linkend="glo_Java"/> keyword.</td> + </tr> + + <tr> + <td><code>B4</code></td> + + <td>yes</td> + + <td>Best practices: Discouraged variable name, non-constant variables should start with lowercase letters.</td> + </tr> + + <tr> + <td><code>AnnualSalary</code></td> + + <td>yes</td> + + <td>Best practices: Legal but discouraged variable name: Non-constant variables should start with lowercase letters.</td> + </tr> + + <tr> + <td><code>"hello"</code></td> + + <td>no</td> + + <td>String delimiters must not be part of an identifier.</td> + </tr> + + <tr> + <td><code>_average </code></td> + + <td>yes</td> + + <td>Best practices: Should be reserved for system code.</td> + </tr> + + <tr> + <td><code>ανδÏος</code></td> + + <td>yes</td> + + <td>Perfectly legal Greek <orgname>Unicode</orgname> characters.</td> + </tr> + + <tr> + <td><code>$sum </code></td> + + <td>yes</td> + + <td>Best practices: Using the <quote>$</quote> sign should be reserved for system code.</td> + </tr> + </informaltable> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + <figure xml:id="sd1_fig_varDeclareBeforeUse"> <title>Declare before use</title> @@ -493,8 +717,7 @@ i = b; // Error: int and boolean are incompatible types i = "Hello"; // Even worse: Assigning a String to an int</programlisting> </figure> - <para><xref linkend="glo_Java"/> provides meta information on - types:</para> + <para><xref linkend="glo_Java"/> provides meta information on types:</para> <figure xml:id="sd1_fig_intTypeMaxMin"> <title>Maximum and minimum values</title> @@ -513,11 +736,9 @@ i = "Hello"; // Even worse: Assigning a String to an int</programlisting> <tr> <td>byte</td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html#BYTES">1</link></td> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html#BYTES">1</link></td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Byte.MIN_VALUE"><inlineequation> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Byte.MIN_VALUE"><inlineequation> <m:math display="inline"> <m:mrow> <m:mo>-</m:mo> @@ -531,8 +752,7 @@ i = "Hello"; // Even worse: Assigning a String to an int</programlisting> </m:math> </inlineequation></link></td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Byte.MAX_VALUE"><inlineequation> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Byte.MAX_VALUE"><inlineequation> <m:math display="inline"> <m:mrow> <m:msup> @@ -552,13 +772,11 @@ i = "Hello"; // Even worse: Assigning a String to an int</programlisting> <tr> <td>char</td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html#BYTES">2</link></td> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html#BYTES">2</link></td> <td>0</td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Character.MAX_VALUE"><inlineequation> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Character.MAX_VALUE"><inlineequation> <m:math display="inline"> <m:mrow> <m:msup> @@ -578,11 +796,9 @@ i = "Hello"; // Even worse: Assigning a String to an int</programlisting> <tr> <td>short</td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Short.BYTES">2</link></td> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Short.BYTES">2</link></td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Short.MIN_VALUE"><inlineequation> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Short.MIN_VALUE"><inlineequation> <m:math display="inline"> <m:mrow> <m:mo>-</m:mo> @@ -596,8 +812,7 @@ i = "Hello"; // Even worse: Assigning a String to an int</programlisting> </m:math> </inlineequation></link></td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Short.MAX_VALUE"><inlineequation> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Short.MAX_VALUE"><inlineequation> <m:math display="inline"> <m:mrow> <m:msup> @@ -617,11 +832,9 @@ i = "Hello"; // Even worse: Assigning a String to an int</programlisting> <tr> <td>int</td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.BYTES">4</link></td> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.BYTES">4</link></td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MIN_VALUE"><inlineequation> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MIN_VALUE"><inlineequation> <m:math display="inline"> <m:mrow> <m:mo>-</m:mo> @@ -635,8 +848,7 @@ i = "Hello"; // Even worse: Assigning a String to an int</programlisting> </m:math> </inlineequation></link></td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE"><inlineequation> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE"><inlineequation> <m:math display="inline"> <m:mrow> <m:msup> @@ -656,11 +868,9 @@ i = "Hello"; // Even worse: Assigning a String to an int</programlisting> <tr> <td>long</td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.BYTES">8</link></td> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.BYTES">8</link></td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MIN_VALUE"><inlineequation> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MIN_VALUE"><inlineequation> <m:math display="inline"> <m:mrow> <m:mo>-</m:mo> @@ -674,8 +884,7 @@ i = "Hello"; // Even worse: Assigning a String to an int</programlisting> </m:math> </inlineequation></link></td> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE"><inlineequation> + <td><link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE"><inlineequation> <m:math display="inline"> <m:mrow> <m:msup> @@ -704,8 +913,7 @@ i = "Hello"; // Even worse: Assigning a String to an int</programlisting> <glossdef> <programlisting language="java">int a = -15;</programlisting> - <para>Possible types: All eight primitive <xref - linkend="glo_Java"/> types.</para> + <para>Possible types: All eight primitive <xref linkend="glo_Java"/> types.</para> </glossdef> </glossentry> @@ -715,8 +923,7 @@ i = "Hello"; // Even worse: Assigning a String to an int</programlisting> <glossdef> <programlisting language="java">GpsPosition start = new GpsPosition(48.7758, 9.1829);</programlisting> - <para>Possible types: Arbitrary built in or user defined - classes.</para> + <para>Possible types: Arbitrary built in or user defined classes.</para> </glossdef> </glossentry> </glosslist> @@ -755,8 +962,7 @@ LocalDate birtday = LocalDate.of(1990, Month.JULY, 5);</programlisting> </tr> <tr> - <td><programlisting language="java">System.out.println("Decimal "+ <emphasis - role="bold">35</emphasis>); + <td><programlisting language="java">System.out.println("Decimal "+ <emphasis role="bold">35</emphasis>); System.out.println("Binary " + <emphasis role="bold">0b10_0011</emphasis>); System.out.println("Hex " + <emphasis role="bold">0x23</emphasis>); System.out.println("Octal " + <emphasis role="bold">043</emphasis>); </programlisting></td> @@ -772,661 +978,370 @@ Octal 35</screen></td> <figure xml:id="sd1_fig_intLiteralLimitExcess"> <title>Know your limits!</title> - <programlisting language="java">System.out.println(10000000000<emphasis - role="bold">L</emphasis>); // o.K. + <programlisting language="java">System.out.println(10000000000<emphasis role="bold">L</emphasis>); // o.K. System.out.println(10000000000 ); // Compile time error: Integer number too large</programlisting> </figure> - <figure xml:id="sd1_fig_javaLiterals"> - <title><xref linkend="glo_Java"/> literals</title> - - <informaltable border="1"> - <tr> - <td><code>byte</code>, <code>short</code></td> - - <td>-</td> - </tr> - - <tr> - <td><code>char</code></td> - - <td><code>'A'</code>, <code>'\u0041'</code></td> - </tr> - - <tr> - <td><code>int</code></td> - - <td><code>29</code>, <code>0b1_1101</code>, <code>0x1D</code>, - <code>035</code>, <code>-29</code>,</td> - </tr> - - <tr> - <td><code>long</code></td> - - <td><code>35L</code>, <code>0b10_0011L</code>, <code>0x23L</code>, - <code>043L</code>, <code>-35L</code>,...</td> - </tr> - - <tr> - <td><code>float</code></td> - - <td><code>55.43F</code>, <code>1.7E-23F</code>, <code>-17.F, - 100_342.334_113</code></td> - </tr> - - <tr> - <td><code>double</code></td> - - <td><code>55.43</code>, <code>1.7 E -23</code>, - <code>-17.</code></td> - </tr> - - <tr> - <td><code>String</code></td> - - <td><code>"Hello"</code>, <code>"Greek Δ"</code>, <code>"Greek - <link - xlink:href="https://www.fileformat.info/info/unicode/char/0394/index.htm">\u0394</link>"</code></td> - </tr> - - <tr> - <td><code>boolean</code></td> - - <td><code>true</code>, <code>false</code></td> - </tr> - - <tr> - <td/> - - <td><code>null</code></td> - </tr> - </informaltable> - </figure> - - <figure xml:id="sd1_fig_romanNumerals"> - <title>Just kidding ...</title> - - <programlisting language="java">int year = MMXIV; // <link - xlink:href="https://en.wikipedia.org/wiki/Roman_numerals">Roman numerals</link> representation - -System.out.println("Olympic winter games: " + year);</programlisting> - - <screen>Olympic winter games: 2014</screen> - </figure> - </section> - - <section xml:id="sd1_sect_arithmeticLimitations"> - <title>Arithmetic limitations</title> - - <para>Expect the unexpected:</para> - - <figure xml:id="sd1_fig_byteLitteralAssign"> - <title>Strange things</title> - - <programlisting language="java">byte count = 91; // o.K. - -byte points = 130; // Compile error: Incompatible types - // Required: byte Found: int</programlisting> - </figure> - - <figure xml:id="sd1_fig_byteOverflow"> - <title>Overflow</title> - - <programlisting language="java">int count = 2147483647; -int points = 2147483647; - -int sum = count + points; -System.out.println("Sum = " + sum);</programlisting> - - <para>Result:</para> - - <informaltable border="1"> - <tr> - <td valign="top"><screen>Sum = -2</screen></td> - - <td><screen> 01111111_11111111_11111111_11111111 -+ 01111111_11111111_11111111_11111111 -_____________________________________ - 11111111_11111111_11111111_11111110</screen></td> - </tr> - </informaltable> - </figure> - - <para>Why not using float / double in favour of bounded - <code>byte</code>, <code>short</code>, <code>int</code>, - <code>long</code> for arithmetics?</para> - - <figure xml:id="sd1_fig_floatPrecision"> - <title>Limited precision</title> - - <programlisting language="java">float float2Power31 = Integer.MAX_VALUE + 1f; - -float floatDoubleMAX_VALUE = 2 * float2Power31* float2Power31 - 1f; // 2^63 - 1 - -System.out.format( " Float value: %f\n", floatDoubleMAX_VALUE); -System.out.println("Expected value: " + Long.MAX_VALUE);</programlisting> - - <para>Result:</para> - - <screen> Float value: 9223372036854776000.000000 -Expected value: 9223372036854775807</screen> - </figure> - - <figure xml:id="sd1_fig_floatConverter"> - <title><uri - xlink:href="https://www.h-schmidt.net/FloatConverter">https://www.h-schmidt.net/FloatConverter</uri></title> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/floatConverter.png"/> - </imageobject> - </mediaobject> - </figure> - </section> - </section> - - <section xml:id="sd1_sect_langFundamental_exercises"> - <title>Exercises</title> - - <section xml:id="sw1LegalVariableName"> - <title>Legal variable names</title> - - <qandaset defaultlabel="qanda" xml:id="sw1QandaLegalVarNames"> - <title>Legal variable names</title> + <qandaset defaultlabel="qanda" xml:id="sw1QandaMaxMinInt"> + <title>An <code>int</code>'s minimum and maximum value</title> <qandadiv> <qandaentry> <question> - <para>Which of the following names are legal variable names? - Complete the following table and explain your decision with - respect to the <quote - xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc4_html_3">variables</quote> - section.</para> + <para>In this exercise we look at an <code>int</code>'s the largest and smallest possible value.</para> - <informaltable border="1"> - <colgroup width="19%"/> - - <colgroup width="10%"/> - - <colgroup width="71%"/> - - <tr> - <th>Identifier</th> - - <th>is legal? (yes / no)</th> - - <th>Explanation / remark</th> - </tr> + <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> - <tr> - <td><code>for</code></td> - - <td/> - - <td/> - </tr> - - <tr> - <td><code>sum_of_data</code></td> - - <td/> - - <td/> - </tr> - - <tr> - <td><code>sumOfData</code></td> - - <td/> - - <td/> - </tr> - - <tr> - <td><code>first-name</code></td> + <m:mi>2</m:mi> + </m:msub> + </m:math> + </inlineequation> for example represents the decimal value 5.</para> - <td/> + <para>In order to represent negative values as well <xref linkend="glo_Java"/> uses <link xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's complement</link> representation. We provide some values:</para> - <td/> - </tr> + <table border="1" xml:id="sw1Table4ByteIntegerRepresentation"> + <caption>4 Byte <link xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's complement</link> representation of <code>int</code> values.</caption> <tr> - <td><code>ABC</code></td> + <th>Two complement representation</th> - <td/> - - <td/> + <th>Decimal representation</th> </tr> <tr> - <td><code>42isThesolution</code></td> - - <td/> + <td><code>00000000000000000000000000000000</code></td> - <td/> + <td>0</td> </tr> <tr> - <td><code>println</code></td> - - <td/> - - <td/> - </tr> + <td><code>01111111111111111111111111111111</code></td> - <tr> - <td><code>B4</code></td> + <td><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <td/> + <m:mrow> + <m:mi>16</m:mi> - <td/> - </tr> + <m:mo>-</m:mo> - <tr> - <td><code>AnnualSalary</code></td> + <m:mi>1</m:mi> + </m:mrow> + </m:msup> - <td/> + <m:mo>-</m:mo> - <td/> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation> (Maximum)</td> </tr> <tr> - <td><code>"hello"</code></td> + <td><code>10000000000000000000000000000000</code></td> - <td/> + <td><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> - <td/> - </tr> + <m:msup> + <m:mi>2</m:mi> - <tr> - <td><code>_average </code></td> + <m:mrow> + <m:mi>16</m:mi> - <td/> + <m:mo>-</m:mo> - <td/> + <m:mi>1</m:mi> + </m:mrow> + </m:msup> + </m:mrow> + </m:math> + </inlineequation> (Minimum)</td> </tr> <tr> - <td><code>ανδÏος</code></td> - - <td/> + <td><code>11111111111111111111111111111111</code></td> - <td/> + <td>-1</td> </tr> + </table> - <tr> - <td><code>$sum </code></td> + <para>Use <link xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1"><code>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>0B1100</code> in section <quote xlink:href="https://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> - <td/> - - <td/> - </tr> - </informaltable> + <programlisting language="java"> public static void main(String[] args) { + + int minumum = ... , //TODO: provide values by + maximum = ...; // binary int literals - <tip> - <para>You may want to prepare a simple <xref - linkend="glo_Java"/> program testing the above names.</para> - </tip> + System.out.println("Minimum:" + minumum); + System.out.println("Maximum:" + maximum); + }</programlisting> </question> <answer> - <para>We may write a small <xref linkend="glo_Java"/> - program:</para> + <para>We insert <link xlink:href="https://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="java">public static void main(String[] args) { - int 42isThesolution = 6; // <emphasis role="bold">Syntax error on token "42", delete this token</emphasis> -}</programlisting> - - <para>Unfortunately the error explanation is not too helpful - with respect to <xref linkend="glo_Java"/> beginners. It does - not immediately point towards an illegal identifier problem. One - indication of a sound compiler is its ability to provide - meaningful, self explanatory error messages.</para> - - <informaltable border="1"> - <colgroup width="19%"/> - - <colgroup width="10%"/> + + int minumum = 0B10000000_00000000_00000000_00000000, + maximum = 0B01111111_11111111_11111111_11111111; - <colgroup width="71%"/> + System.out.println("Minimum int value:" + minumum); + System.out.println("Maximum int value:" + maximum); + }</programlisting> - <tr> - <th>Identifier</th> + <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> - <th>is legal? (yes / no)</th> + <programlisting language="java"> 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> + </qandaset> - <th>Explanation / remark</th> - </tr> + <qandaset defaultlabel="qanda" xml:id="sw1QandaIntLimitShort"> + <title>Testing the limits (Difficult)</title> - <tr> - <td><code>for</code></td> - - <td>no</td> + <qandadiv> + <qandaentry> + <question> + <para>A careful programmer is worried whether a short variable is <emphasis role="bold">always</emphasis> large enough to hold color intensity values ranging from 0 to 255. Give an answer being based on the <link xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.1">specification</link> and not just by <acronym>try-and-error</acronym>.</para> - <td><quote>for</quote> is a Java keyword.</td> - </tr> + <para>The programmer tries to adopt the <link linkend="sw1QandaMaxMinInt">related int example</link> accordingly:</para> - <tr> - <td><code>sum_of_data</code></td> + <programlisting language="java">short minumum = 0B10000000_00000000, + maximum = 0B01111111_11111111; - <td>yes</td> +System.out.println("Minimum short value:" + minumum); +System.out.println("Maximum short value:" + maximum</programlisting> - <td>-</td> - </tr> + <para>Our programmer is baffled:</para> - <tr> - <td><code>sumOfData</code></td> + <itemizedlist> + <listitem> + <para>The first assignment <code language="java">minumum = 0B10000000_00000000</code> is being flagged as a compile time error:</para> - <td>yes</td> + <para><computeroutput>Type mismatch: cannot convert from int to short</computeroutput></para> + </listitem> - <td>-</td> - </tr> + <listitem> + <para>On contrary the second assignment <code language="java">maximum = 0B01111111_11111111</code> gets smoothly accepted.</para> + </listitem> + </itemizedlist> - <tr> - <td><code>first-name</code></td> + <tip> + <para>Which integer literal types do exist according to the <xref linkend="glo_Java"/> <link xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1">standard</link>?</para> + </tip> + </question> - <td>no</td> + <answer> + <para>Since variables of type <code>short</code> have a two bytes representation their corresponding range is <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>[</m:mo> - <td>Operators like <quote>-</quote> or <quote>+</quote> must - not appear in variable names.</td> - </tr> + <m:mrow> + <m:mrow> + <m:mo>-</m:mo> - <tr> - <td><code>ABC</code></td> + <m:msup> + <m:mi>2</m:mi> - <td>yes</td> + <m:mi>15</m:mi> + </m:msup> + </m:mrow> - <td>Best practices: Discouraged variable name, non-constant - variables should start with lowercase letters.</td> - </tr> + <m:mo>,</m:mo> - <tr> - <td><code>42isThesolution</code></td> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <td>no</td> + <m:mi>15</m:mi> + </m:msup> - <td>Identifiers must not start with a number.</td> - </tr> + <m:mo>-</m:mo> - <tr> - <td><code>println</code></td> + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> - <td>yes</td> + <m:mo>]</m:mo> + </m:mrow> + </m:math> + </inlineequation>. Thus intensity values ranging from 0 to 255 will be easily accommodated.</para> - <td><code>println</code> is a method of - <code>System.out.println(...)</code> but is no <xref - linkend="glo_Java"/> keyword.</td> - </tr> + <para>The second question is more difficult to explain: The <xref linkend="glo_Java"/> standard only defines <code>int</code> literals. It does however not define <code>short</code> (or <code>byte</code>) literals.</para> - <tr> - <td><code>B4</code></td> + <para>Thus the 0B01111111_11111111 literal will be treated as an <code>int</code> value being compatible with a <code>short</code> variable and is thus assignable.</para> - <td>yes</td> + <para>On the contrary the binary value <code>10000000_00000000</code> evaluates to <inlineequation> + <m:math display="inline"> + <m:msup> + <m:mn>2</m:mn> - <td>Best practices: Discouraged variable name, non-constant - variables should start with lowercase letters.</td> - </tr> + <m:mi>15</m:mi> + </m:msup> + </m:math> + </inlineequation> (equal to 32768) exceeding a two-byte (signed) short variable's upper limit.</para> - <tr> - <td><code>AnnualSalary</code></td> + <para>Even worse: A <code>short</code> variable containing binary <code>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"> + <m:mrow> + <m:mo>-</m:mo> - <td>yes</td> + <m:msup> + <m:mn>2</m:mn> - <td>Best practices: Legal but discouraged variable name: - Non-constant variables should start with lowercase - letters.</td> - </tr> + <m:mi>15</m:mi> + </m:msup> + </m:mrow> + </m:math> + </inlineequation> (or -32768).</para> - <tr> - <td><code>"hello"</code></td> + <para>In order to avoid this error we need an explicit type conversion (a so called cast):</para> - <td>no</td> + <programlisting language="java">short minumum = (short) 0B10000000_00000000;</programlisting> - <td>String delimiters must not be part of an - identifier.</td> - </tr> + <para>Since a <code>short</code> variable's range is <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>[</m:mo> - <tr> - <td><code>_average </code></td> + <m:mrow> + <m:mrow> + <m:mo>-</m:mo> - <td>yes</td> + <m:msup> + <m:mi>2</m:mi> - <td>Best practices: Should be reserved for system code.</td> - </tr> + <m:mi>15</m:mi> + </m:msup> + </m:mrow> - <tr> - <td><code>ανδÏος</code></td> + <m:mo>,</m:mo> - <td>yes</td> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <td>Perfectly legal Greek <orgname>Unicode</orgname> - characters.</td> - </tr> + <m:mi>15</m:mi> + </m:msup> - <tr> - <td><code>$sum </code></td> + <m:mo>-</m:mo> - <td>yes</td> + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> - <td>Best practices: Using the <quote>$</quote> sign should - be reserved for system code.</td> - </tr> - </informaltable> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <m:mo>]</m:mo> + </m:mrow> + </m:math> + </inlineequation> we may as well use a unary minus sign instead:</para> - <section xml:id="sw1IntegerLiterals"> - <title>Integer value literals</title> + <programlisting language="java">short minumum = -0B10000000_00000000;</programlisting> - <section xml:id="sd1SectBinaryIntLiteral"> - <title>Binary literals</title> + <para>Both variants are syntactically correct and will thus compile. However they are likely not being intended by our programmer.</para> - <qandaset defaultlabel="qanda" xml:id="sd1QandaBinaryIntLiteral"> - <qandadiv> - <qandaentry> - <question> - <orderedlist> - <listitem> - <para>Using the decimal system to represent integer values - we tend to ignore other important numbering systems. Write - an application which prints the decimal value of - <inlineequation> - <m:math display="inline"> - <m:msub> - <m:mi>1110100100</m:mi> + <para>The error in the first place is probably assigning a value of <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>+</m:mo> - <m:mi>2</m:mi> - </m:msub> - </m:math> - </inlineequation> by writing the former as a binary int - literal. Verify the printed value by an independent - calculation.</para> - </listitem> + <m:msup> + <m:mi>2</m:mi> - <listitem> - <para>Construct a second binary literal representing a - negative value of your choice.</para> + <m:mi>15</m:mi> + </m:msup> + </m:mrow> + </m:math> + </inlineequation> to a <code>short</code> variable at all.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <tip> - <para>An <code>int</code> is being represented by four - bytes in <xref linkend="glo_Java"/>.</para> - </tip> - </listitem> - </orderedlist> - </question> + <figure xml:id="sd1_fig_javaLiterals"> + <title><xref linkend="glo_Java"/> literals</title> - <answer> - <orderedlist> - <listitem> - <programlisting language="java">public static void main(String[] args) { + <informaltable border="1"> + <tr> + <td><code>byte</code>, <code>short</code></td> - System.out.println(" Binary:" + 0B11_10100100); - System.out.println("Decimal:" + (512 - + 256 - + 128 - + 32 - + 4)); - }</programlisting> + <td>-</td> + </tr> - <para>This yields:</para> + <tr> + <td><code>char</code></td> - <screen> Binary:932 -Decimal:932</screen> - </listitem> + <td><code>'A'</code>, <code>'\u0041'</code></td> + </tr> - <listitem> - <para>A negative value in <link - xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representation starts with a - <quote>1</quote> at its highest bit. Binary literals in - <xref linkend="glo_Java"/> represent <code>int</code> - values. An <code>int</code> in <xref linkend="glo_Java"/> - uses 4 bytes and thus occupies 4 x 8=32 bits. Therefore - choosing a negative value is a simple task: Start with - <quote>1</quote> and let it follow by 32 - 1 = 31 random - bit values:</para> + <tr> + <td><code>int</code></td> - <programlisting language="java">public static void main(String[] args) { - System.out.println(0B10000000_00111001_01101001_01110100); -}</programlisting> - </listitem> - </orderedlist> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <td><code>29</code>, <code>0b1_1101</code>, <code>0x1D</code>, <code>035</code>, <code>-29</code>,</td> + </tr> - <section xml:id="sd1SectBracesInPrintln"> - <title>Why using braces inside <code>System.out.println(...)</code> - ?</title> + <tr> + <td><code>long</code></td> - <qandaset defaultlabel="qanda" xml:id="sd1QandaBracesInPrintln"> - <qandadiv> - <qandaentry> - <question> - <para>Solution <xref linkend="sd1SectBinaryIntLiteral"/> - contains:</para> + <td><code>35L</code>, <code>0b10_0011L</code>, <code>0x23L</code>, <code>043L</code>, <code>-35L</code>,...</td> + </tr> - <programlisting language="java">... - System.out.println("Decimal:" + (512 + - 256 + - 128 + - 32 + - 4)); ...</programlisting> + <tr> + <td><code>float</code></td> - <para>Why are the <quote>inner</quote> braces immediately - preceding 512 and following 4 are being required?</para> + <td><code>55.43F</code>, <code>1.7E-23F</code>, <code>-17.F, 100_342.334_113</code></td> + </tr> - <tip> - <itemizedlist> - <listitem> - <para>Execute the above code omitting the - <quote>inner</quote> braces.</para> - </listitem> + <tr> + <td><code>double</code></td> - <listitem> - <para>Read about the <quote>+</quote> operator's role - e.g. in <code>System.out.println("Some" + " - string").</code></para> - </listitem> - </itemizedlist> - </tip> - </question> + <td><code>55.43</code>, <code>1.7 E -23</code>, <code>-17.</code></td> + </tr> - <answer> - <para>We start omitting the <quote>inner</quote> - braces:</para> + <tr> + <td><code>String</code></td> - <programlisting language="java">... - System.out.println("Decimal:" + 512 + - 256 + - 128 + - 32 + - 4); ...</programlisting> + <td><code>"Hello"</code>, <code>"Greek Δ"</code>, <code>"Greek <link xlink:href="https://www.fileformat.info/info/unicode/char/0394/index.htm">\u0394</link>"</code></td> + </tr> - <para>This results in the following output:</para> + <tr> + <td><code>boolean</code></td> - <screen>... -Decimal:512256128324</screen> + <td><code>true</code>, <code>false</code></td> + </tr> - <para>The numbers are being treated as strings rather than - integer values The above code is equivalent to:</para> - - <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>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> - - <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"/> - 128 +<co - linkends="sd1ListingPlusOpDuplicate-2" - xml:id="sd1ListingPlusOpDuplicate-4-co"/> - 32 +<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>"Decimal:"</code> and - <code>"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>"932"</code> in order to be compatible with the - preceding <code>"Decimal:"</code> string.</para> - </callout> - </calloutlist> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <tr> + <td/> + + <td><code>null</code></td> + </tr> + </informaltable> + </figure> - <section xml:id="sd1_sect_poorMansAscii"> + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_poorMansAscii"> <title>Poor mans ASCII table</title> - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_poorMansAscii"> - <qandadiv> - <qandaentry> - <question> - <para>We want to construct a list of <link - 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 character literals - <code>' '</code>, <code>'!'</code>, <code>'"'</code>, - <code>'#'</code> and so on to show their corresponding decimal - values. The intended output is:</para> - - <screen> : 32 + <qandadiv> + <qandaentry> + <question> + <para>We want to construct a list of <link 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 character literals <code>' '</code>, <code>'!'</code>, <code>'"'</code>, <code>'#'</code> and so on to show their corresponding decimal values. The intended output is:</para> + + <screen> : 32 !: 33 ": 34 #: 35 @@ -1434,21 +1349,17 @@ $: 36 %: 37 &: 38</screen> - <para>Notice the empty space being represented by decimal - 32.</para> + <para>Notice the empty space being represented by decimal 32.</para> - <tip> - <para>A <code>char</code> value being represented by two - bytes may be easily assigned to an <code>int</code> - variable.</para> - </tip> - </question> + <tip> + <para>A <code>char</code> value being represented by two bytes may be easily assigned to an <code>int</code> variable.</para> + </tip> + </question> - <answer> - <para>Since <code>char</code> values may be assigned to - <code>int</code> variables we may code:</para> + <answer> + <para>Since <code>char</code> values may be assigned to <code>int</code> variables we may code:</para> - <programlisting language="java">{ + <programlisting language="java">{ char c = ' '; // space int value = c; System.out.println(c + ": " + value); @@ -1465,10 +1376,9 @@ $: 36 } ...</programlisting> - <para>Using an explicit type conversion from char to int (a so - called cast) yields an identical result:</para> + <para>Using an explicit type conversion from char to int (a so called cast) yields an identical result:</para> - <programlisting language="java">System.out.println(' ' + ": " + ((int) ' ')); + <programlisting language="java">System.out.println(' ' + ": " + ((int) ' ')); System.out.println('!' + ": " + ((int) '!')); System.out.println('"' + ": " + ((int) '"')); System.out.println('#' + ": " + ((int) '#')); @@ -1476,36 +1386,24 @@ System.out.println('$' + ": " + ((int) '$')); System.out.println('%' + ": " + ((int) '%')); System.out.println('&' + ": " + ((int) '&')); ...</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaIntByBinary"> + <title>Integer value hexadecimal representation</title> + + <qandadiv> + <qandaentry> + <question> + <para>As you may know the <xref linkend="glo_RGB"/> color model uses triplets of numbers to define color value components representing intensities of its three base colors <foreignphrase><emphasis role="bold">R</emphasis>ed</foreignphrase>, <foreignphrase><emphasis role="bold">G</emphasis>reen</foreignphrase> and <foreignphrase><emphasis role="bold">B</emphasis>lue</foreignphrase>. The component values range from 0 to 255, the latter defining maximum intensity.</para> - <section xml:id="sw1SectHexadecimalLiterals"> - <title>Hexadecimal literals</title> - - <para>As you may know the <xref linkend="glo_RGB"/> color model uses - triplets of numbers to define color value components representing - intensities of its three base colors <foreignphrase><emphasis - role="bold">R</emphasis>ed</foreignphrase>, <foreignphrase><emphasis - role="bold">G</emphasis>reen</foreignphrase> and - <foreignphrase><emphasis role="bold">B</emphasis>lue</foreignphrase>. - The component values range from 0 to 255, the latter defining maximum - intensity.</para> - - <para>The color <quote>red</quote> for example is being represented by - (255, 0, 0). So the red component has maximum intensity while blue and - green are zero.</para> - - <para>It is however common to use <link - xlink:href="http://www.december.com/html/spec/color16codes.html">hexadecimal - in favour of decimal values</link>. Thus the same color - <quote>red</quote> in the subsequent HTML example's heading font is - now being represented by <emphasis - role="bold">(FF,0,0)</emphasis>:</para> - - <programlisting language="html"><!DOCTYPE html> + <para>The color <quote>red</quote> for example is being represented by (255, 0, 0). So the red component has maximum intensity while blue and green are zero.</para> + + <para>It is however common to use <link xlink:href="http://www.december.com/html/spec/color16codes.html">hexadecimal in favour of decimal values</link>. Thus the same color <quote>red</quote> in the subsequent HTML example's heading font is now being represented by <emphasis role="bold">(FF,0,0)</emphasis>:</para> + + <programlisting language="html"><!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>A simple color example</title> @@ -1514,585 +1412,476 @@ System.out.println('&' + ": " + ((int) '&')); <body><h1 style="<emphasis role="bold">color: #FF0000;</emphasis>">My heading</h1></body> </html></programlisting> - <qandaset defaultlabel="qanda" xml:id="sw1QandaIntByBinary"> - <title>Integer values by binary representations</title> - - <qandadiv> - <qandaentry> - <question> - <para>Write a program printing the hexadecimal representation - <inlineequation> - <m:math display="inline"> - <m:msub> - <m:mi>C0</m:mi> - - <m:mi>16</m:mi> - </m:msub> - </m:math> - </inlineequation> as a decimal value.</para> - - <para>Complete the following code by assigning the hexadecimal - value <inlineequation> - <m:math display="inline"> - <m:msub> - <m:mi>C0</m:mi> - - <m:mi>16</m:mi> - </m:msub> - </m:math> - </inlineequation> (The <quote>silver</quote> color's all - three component's intensity in <uri - xlink:href="http://www.december.com/html/spec/color16codes.html">http://www.december.com/html/spec/color16codes.html</uri>) - to get the output in decimal representation.</para> - - <programlisting language="java">public static void main(String[] args) { + <para>Write a program printing the hexadecimal representation <inlineequation> + <m:math display="inline"> + <m:msub> + <m:mi>C0</m:mi> + + <m:mi>16</m:mi> + </m:msub> + </m:math> + </inlineequation> as a decimal value.</para> + + <para>Complete the following code by assigning the hexadecimal value <inlineequation> + <m:math display="inline"> + <m:msub> + <m:mi>C0</m:mi> + + <m:mi>16</m:mi> + </m:msub> + </m:math> + </inlineequation> (The <quote>silver</quote> color's all three component's intensity in <uri xlink:href="http://www.december.com/html/spec/color16codes.html">http://www.december.com/html/spec/color16codes.html</uri>) to get the output in decimal representation.</para> + + <programlisting language="java">public static void main(String[] args) { short intensity = ...; System.out.println(intensity); }</programlisting> - <tip> - <para>You may want to consider the <quote - xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc6_html_2">Literals</quote> - section of <xref linkend="bib_Kurniawan2015"/> learning - about hexadecimal integer value representation.</para> - </tip> - </question> + <tip> + <para>You may want to consider the <quote xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc6_html_2">Literals</quote> section of <xref linkend="bib_Kurniawan2015"/> learning about hexadecimal integer value representation.</para> + </tip> + </question> - <answer> - <para>The solution is straightforward:</para> + <answer> + <para>Using hexadecimal literals we have:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java">short intensity = 0xC0; - short intensity = 0xC0; - - System.out.println(intensity); +System.out.println(intensity);</programlisting> + </answer> + </qandaentry> + </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> is meant to be octal representation. Unfortunately the only digits in octal representations range from 0 to 7. So <quote>047</quote> is fine but <quote>048</quote> simply is no valid literal in <xref linkend="glo_Java"/>.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <figure xml:id="sd1_fig_romanNumerals"> + <title>Just kidding ...</title> + + <programlisting language="java">int year = MMXIV; // <link xlink:href="https://en.wikipedia.org/wiki/Roman_numerals">Roman numerals</link> representation + +System.out.println("Olympic winter games: " + year);</programlisting> + + <screen>Olympic winter games: 2014</screen> + </figure> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaBinaryIntLiteral"> + <title>Binary literals</title> + + <qandadiv> + <qandaentry> + <question> + <orderedlist> + <listitem> + <para>Using the decimal system to represent integer values we tend to ignore other important numbering systems. Write an application which prints the decimal value of <inlineequation> + <m:math display="inline"> + <m:msub> + <m:mi>1110100100</m:mi> + + <m:mi>2</m:mi> + </m:msub> + </m:math> + </inlineequation> by writing the former as a binary int literal. Verify the printed value by an independent calculation.</para> + </listitem> + + <listitem> + <para>Construct a second binary literal representing a negative value of your choice.</para> + + <tip> + <para>An <code>int</code> is being represented by four bytes in <xref linkend="glo_Java"/>.</para> + </tip> + </listitem> + </orderedlist> + </question> + + <answer> + <orderedlist> + <listitem> + <programlisting language="java">public static void main(String[] args) { + + System.out.println(" Binary:" + 0B11_10100100); + System.out.println("Decimal:" + (512 + + 256 + + 128 + + 32 + + 4)); }</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - - <qandaset defaultlabel="qanda" xml:id="sw1QandaIntLimitShort"> - <title>Testing the limits, part 2 (Difficult)</title> - - <qandadiv> - <qandaentry> - <question> - <para>A careful programmer is worried whether a short variable - is <emphasis role="bold">always</emphasis> large enough to - hold color intensity values ranging from 0 to 255. Give an - answer being based on the <link - xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.1">specification</link> - and not just by try-and-error.</para> - - <para>The programmer tries to adopt the <link - linkend="sw1QandaMaxMinInt">related int example</link> - accordingly:</para> - - <programlisting language="java">short minumum = 0B10000000_00000000, - maximum = 0B01111111_11111111; -System.out.println("Minimum short value:" + minumum); -System.out.println("Maximum short value:" + maximum); -</programlisting> + <para>This yields:</para> - <para>Our programmer is baffled since:</para> + <screen> Binary:932 +Decimal:932</screen> + </listitem> + + <listitem> + <para>A negative value in <link xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's complement</link> representation starts with a <quote>1</quote> at its highest bit. Binary literals in <xref linkend="glo_Java"/> represent <code>int</code> values. An <code>int</code> in <xref linkend="glo_Java"/> uses 4 bytes and thus occupies 4 x 8=32 bits. Therefore choosing a negative value is a simple task: Start with <quote>1</quote> and let it follow by 32 - 1 = 31 random bit values:</para> + + <programlisting language="java">public static void main(String[] args) { + System.out.println(0B10000000_00111001_01101001_01110100); +}</programlisting> + </listitem> + </orderedlist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaBracesInPrintln"> + <title>Why using braces inside <code>System.out.println(...)</code> ?</title> + + <qandadiv> + <qandaentry> + <question> + <para>Solution <xref linkend="sd1QandaBinaryIntLiteral"/> contains:</para> + + <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> + + <tip> <itemizedlist> <listitem> - <para>The first assignment <code language="java">minumum = - 0B10000000_00000000</code> is being flagged as a compile - time error:</para> - - <para><computeroutput>Type mismatch: cannot convert from - int to short</computeroutput></para> + <para>Execute the above code omitting the <quote>inner</quote> braces.</para> </listitem> <listitem> - <para>On contrary the second assignment <code - language="java">maximum = 0B01111111_11111111</code> gets - smoothly accepted.</para> + <para>Read about the <quote>+</quote> operator's role e.g. in <code>System.out.println("Some" + " string").</code></para> </listitem> </itemizedlist> + </tip> + </question> - <tip> - <para>Which integer literals types do exist according to the - <xref linkend="glo_Java"/> <link - xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1">standard</link>?</para> - </tip> - </question> - - <answer> - <para>Since variables of type <code>short</code> are being - represented by to bytes their range is <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>[</m:mo> + <answer> + <para>We start omitting the <quote>inner</quote> braces:</para> - <m:mrow> - <m:mrow> - <m:mo>-</m:mo> + <programlisting language="java">... +System.out.println("Decimal:" + 512 + + 256 + + 128 + + 32 + + 4); ...</programlisting> - <m:msup> - <m:mi>2</m:mi> + <para>This results in the following output:</para> - <m:mi>15</m:mi> - </m:msup> - </m:mrow> + <screen>... +Decimal:512256128324</screen> - <m:mo>,</m:mo> + <para>The numbers are being treated as strings rather than integer values The above code is equivalent to:</para> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <programlisting language="java">... +System.out.println("Decimal:" + "512" + + "256" + + "128" + + "32" + + "4"); ...</programlisting> - <m:mi>15</m:mi> - </m:msup> + <para>The <quote>+</quote> operator between two strings defines their concatenation. So all six components get joined into a single result string.</para> - <m:mo>-</m:mo> + <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> - <m:mi>1</m:mi> - </m:mrow> - </m:mrow> + <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"/> + 128 +<co linkends="sd1ListingPlusOpDuplicate-2" xml:id="sd1ListingPlusOpDuplicate-4-co"/> + 32 +<co linkends="sd1ListingPlusOpDuplicate-2" xml:id="sd1ListingPlusOpDuplicate-5-co"/> + 4)); ...</programlisting> - <m:mo>]</m:mo> - </m:mrow> - </m:math> - </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 linkend="glo_Java"/> standard does define - <code>int</code> literals. It does however not define - <code>short</code> (or <code>byte</code>) literals.</para> - - <para>Thus the 0B01111111_11111111 literal will be treated as - an <code>int</code> value being compatible with a - <code>short</code> variable and is thus assignable.</para> - - <para>On the contrary the binary value - <code>10000000_00000000</code> evaluates to <inlineequation> - <m:math display="inline"> - <m:msup> - <m:mn>2</m:mn> + <calloutlist> + <callout arearefs="sd1ListingPlusOpDuplicate-1-co" xml:id="sd1ListingPlusOpDuplicate-1"> + <para><quote>+</quote> operator concatenating the two strings <code>"Decimal:"</code> and <code>"932"</code>.</para> + </callout> - <m:mi>15</m:mi> - </m:msup> - </m:math> - </inlineequation> (equal to 32768) exceeding a two-byte - short variable's upper limit.</para> - - <para>Even worse: A <code>short</code> variable containing - binary <code>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"> - <m:mrow> - <m:mo>-</m:mo> + <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>"932"</code> in order to be compatible with the preceding <code>"Decimal:"</code> string.</para> + </callout> + </calloutlist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_exerciseEmoticons"> + <title><xref linkend="glo_unicode"/> and emoticons</title> + + <qandadiv> + <qandaentry> + <question> + <para>Referring to <xref linkend="sd1_fig_unicodeSample"/> we may use a corresponding <link xlink:href="https://en.wikibooks.org/wiki/Java_Programming/Literals#Character_Literals">character literal</link> representing Arabic:</para> + + <programlisting language="java">final char arabicChar = 'Úœ'; // A <link xlink:href="https://en.wikibooks.org/wiki/Java_Programming/Literals#Character_Literals">character literal</link> +System.out.println("Character: " + arabicChar);</programlisting> + + <para>Execution yields the expected output. On contrary the following code snippet using the smiley instead does not even compile:</para> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/emoticonInvalidCharacterConstant.png"/> + </imageobject> + </mediaobject> + + <para>Explain the underlying problem.</para> + + <tip> + <para>Consider the number of bytes being allocated for <code>char</code> values in <xref linkend="glo_Java"/>.</para> + </tip> + </question> + + <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>'Úœ'</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>char</code> values. Thus the largest representable code point value is <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mrow> <m:msup> - <m:mn>2</m:mn> + <m:mi>2</m:mi> - <m:mi>15</m:mi> + <m:mi>16</m:mi> </m:msup> - </m:mrow> - </m:math> - </inlineequation> (or -32768).</para> - <para>In order to avoid this error we need an explicit type - conversion (a so called cast):</para> + <m:mo>-</m:mo> - <programlisting language="java">short minumum = (short) 0B10000000_00000000;</programlisting> + <m:mi>1</m:mi> + </m:mrow> - <para>Since a <code>short</code> variable's range is - <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>[</m:mo> + <m:mo>=</m:mo> - <m:mrow> - <m:mrow> - <m:mo>-</m:mo> + <m:mi>65535</m:mi> + </m:mrow> + </m:math> + </inlineequation>. So <code>'Úœ'</code> fits well into this range.</para> - <m:msup> - <m:mi>2</m:mi> + <para>According to the <link xlink:href="https://en.wikipedia.org/wiki/Emoticons_(Unicode_block)">Emoticons table</link> the smiley's code point value is U+1F60E being equivalent to 128526 decimal. This exceeds the limit of 65535 and thus prohibits the character literal in question.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> - <m:mi>15</m:mi> - </m:msup> - </m:mrow> + <section xml:id="sd1_sect_arithmeticLimitations"> + <title>Arithmetic limitations</title> - <m:mo>,</m:mo> + <para>Expect the unexpected:</para> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <figure xml:id="sd1_fig_byteLitteralAssign"> + <title>Strange things</title> - <m:mi>15</m:mi> - </m:msup> + <programlisting language="java">byte count = 91; // o.K. - <m:mo>-</m:mo> +byte points = 130; // Compile error: Incompatible types + // Required: byte Found: int</programlisting> + </figure> - <m:mi>1</m:mi> - </m:mrow> - </m:mrow> + <figure xml:id="sd1_fig_byteOverflow"> + <title>Overflow</title> - <m:mo>]</m:mo> - </m:mrow> - </m:math> - </inlineequation> we may as well use a unary minus sign - instead:</para> + <programlisting language="java">int count = 2147483647; +int points = 2147483647; - <programlisting language="java">short minumum = -0B10000000_00000000;</programlisting> +int sum = count + points; +System.out.println("Sum = " + sum);</programlisting> - <para>Both variants are syntactically correct and will thus - compile. However they are likely not being intended by our - programmer.</para> + <para>Result:</para> - <para>The error in the first place is probably assigning a - value of <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>+</m:mo> + <informaltable border="1"> + <tr> + <td valign="top"><screen>Sum = -2</screen></td> - <m:msup> - <m:mi>2</m:mi> + <td><screen> 01111111_11111111_11111111_11111111 ++ 01111111_11111111_11111111_11111111 +_____________________________________ + 11111111_11111111_11111111_11111110</screen></td> + </tr> + </informaltable> + </figure> - <m:mi>15</m:mi> - </m:msup> - </m:mrow> - </m:math> - </inlineequation> to a <code>short</code> variable at - all.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <para>Why not using float / double in favour of bounded <code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code> for arithmetics?</para> - <section xml:id="sd1_sect_exerciseEmoticons"> - <title><xref linkend="glo_unicode"/> and emoticons</title> + <figure xml:id="sd1_fig_floatPrecision"> + <title>Limited precision</title> - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_exerciseEmoticons"> - <qandadiv> - <qandaentry> - <question> - <para>Referring to <xref linkend="sd1_fig_unicodeSample"/> we - may use a corresponding <link - xlink:href="https://en.wikibooks.org/wiki/Java_Programming/Literals#Character_Literals">character - literal</link>:</para> - - <programlisting language="java">final char arabicChar = 'Úœ'; // A <link - xlink:href="https://en.wikibooks.org/wiki/Java_Programming/Literals#Character_Literals">character literal</link> -System.out.println("Character: " + arabicChar);</programlisting> + <programlisting language="java">float float2Power31 = Integer.MAX_VALUE + 1f; - <para>Execution yields the expected output. On contrary the - following code snippet using the smiley instead does not even - compile:</para> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/emoticonInvalidCharacterConstant.png"/> - </imageobject> - </mediaobject> - - <para>Explain the underlying problem.</para> - - <tip> - <para>Consider the number of bytes being allocated for - <code>char</code> values in <xref - linkend="glo_Java"/>.</para> - </tip> - </question> - - <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>'Úœ'</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>char</code> values. - Thus the largest representable code point value is - <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> +float floatDoubleMAX_VALUE = 2 * float2Power31* float2Power31 - 1f; // 2^63 - 1 - <m:mi>16</m:mi> - </m:msup> +System.out.format( " Float value: %f\n", floatDoubleMAX_VALUE); +System.out.println("Expected value: " + Long.MAX_VALUE);</programlisting> - <m:mo>-</m:mo> + <para>Result:</para> - <m:mi>1</m:mi> - </m:mrow> + <screen> Float value: 9223372036854776000.000000 +Expected value: 9223372036854775807</screen> + </figure> - <m:mo>=</m:mo> + <figure xml:id="sd1_fig_floatConverter"> + <title><uri xlink:href="https://www.h-schmidt.net/FloatConverter">https://www.h-schmidt.net/FloatConverter</uri></title> - <m:mi>65535</m:mi> - </m:mrow> - </m:math> - </inlineequation>. So <code>'Úœ'</code> fits well into this - range.</para> - - <para>According to the <link - xlink:href="https://en.wikipedia.org/wiki/Emoticons_(Unicode_block)">Emoticons - table</link> the smiley's code point value is U+1F60E being - equivalent to 128526 decimal. This exceeds the limit of 65535 - and thus prohibits the character literal in question.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/floatConverter.png"/> + </imageobject> + </mediaobject> + </figure> + </section> + </section> - <section xml:id="sd1SectIntMinMax"> - <title>An <code>int</code>'s minimum and maximum value</title> + <section xml:id="sd1_sect_langFundamental_exercises"> + <title>Exercises</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> + <section xml:id="sw1SectSimpleExpressions"> + <title>Simple expressions</title> - <m:mi>2</m:mi> - </m:msub> - </m:math> - </inlineequation> for example represents the decimal value - 5.</para> + <qandaset defaultlabel="qanda" xml:id="sw1QandaGuessingResults"> + <title>Guessing results</title> - <para>In order to represent negative values as well <xref - linkend="glo_Java"/> uses <link - xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representation. We provide some - values:</para> + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code segment:</para> - <table border="1" xml:id="sw1Table4ByteIntegerRepresentation"> - <caption>4 Byte <link - xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representation of <code>int</code> - values.</caption> + <programlisting language="java"> int a = 3; + a++; //Incrementing a by 1 --> a==4 - <tr> - <th>Two complement representation</th> + int b = a; // TODO - <th>Decimal representation</th> - </tr> + b--; // TODO + --b; // TODO - <tr> - <td><code>00000000000000000000000000000000</code></td> + int c = b; // TODO - <td>0</td> - </tr> + b = ++a; // TODO + int e = a++; // TODO - <tr> - <td><code>01111111111111111111111111111111</code></td> + a *= b; // TODO - <td><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + System.out.println("a=" + a); + System.out.println("b=" + b); + System.out.println("c=" + c); + System.out.println("e=" + e);</programlisting> - <m:mrow> - <m:mi>16</m:mi> + <para>We want to guess the expected result. Copy/paste the above code into a main() method body within your IDE of choice (probably Eclipse).</para> - <m:mo>-</m:mo> + <para><emphasis role="bold">Do not execute this code yet</emphasis> but replace the TODO comments by guessing the respective variable's value.</para> - <m:mi>1</m:mi> - </m:mrow> - </m:msup> + <para><computeroutput>a=...</computeroutput></para> - <m:mo>-</m:mo> + <para><computeroutput>b=...</computeroutput></para> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation> (Maximum)</td> - </tr> + <para><computeroutput>c=...</computeroutput></para> - <tr> - <td><code>10000000000000000000000000000000</code></td> + <para><computeroutput>e=...</computeroutput></para> - <td><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>-</m:mo> + <para>After finishing your guesses execute your code and start wailing about your success rate.</para> - <m:msup> - <m:mi>2</m:mi> + <tip> + <para>Both <code>x++</code> (postfix notation) and <code>++x</code> (infix notation) are expressions themselves which might even be rewritten as <code>(x++)</code> and <code>(++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> - <m:mrow> - <m:mi>16</m:mi> + <answer> + <para>As inferred by the hint the biggest problem is about understanding postfix and infix notation of the operators <code>++</code> and <code>--</code>. A corresponding expression evaluates to:</para> - <m:mo>-</m:mo> + <itemizedlist> + <listitem> + <para><code>a = x++</code> yields a ==x</para> + </listitem> - <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="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1"><code>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>0B1100</code> in section - <quote - xlink:href="https://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="java"> public static void main(String[] args) { - - int minumum = ... , //TODO: provide values by - maximum = ...; // binary int literals + <listitem> + <para><code>a = ++x</code> yields a ==(x + 1)</para> + </listitem> + </itemizedlist> - System.out.println("Minimum:" + minumum); - System.out.println("Maximum:" + maximum); - }</programlisting> - </question> + <para>The rest is just obeying the <quote>due diligence</quote> rule set:</para> - <answer> - <para>We insert <link - xlink:href="https://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="java"> int a = 3; + a++; //Incrementing a by 1 --> a==4 - <programlisting language="java">public static void main(String[] args) { - - int minumum = 0B10000000_00000000_00000000_00000000, - maximum = 0B01111111_11111111_11111111_11111111; + int b = a; // Assigning value of a --> b==4 - System.out.println("Minimum int value:" + minumum); - System.out.println("Maximum int value:" + maximum); - }</programlisting> + b--; // Decrementing b by 1 --> b==3 + --b; // Decrementing b by 1 --> b==2 - <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="java"> 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> - </qandaset> - </section> + int c = b; // c == 2; - <section xml:id="sd1_sect_literalProblem"> - <title>Literal problem</title> + b = ++a; // Incrementing a by 1 -->a==5, then assigning to b --> b == 5 + int e = a++; // Assigning a to e --> e==5, then incrementing a --> a==6 - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_literalProblem"> - <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> is meant to be octal - representation. Unfortunately the only digits in octal - representations range from 0 to 7. So <quote>047</quote> is - fine but <quote>048</quote> simply is no valid literal in - <xref linkend="glo_Java"/>.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - </section> + a *= b; // Multiplying a with b and assigning the result to a --> a==30 - <section xml:id="sw1SectSimpleExpressions"> - <title>Simple expressions</title> + System.out.println("a=" + a); + System.out.println("b=" + b); + System.out.println("c=" + c); + System.out.println("e=" + e);</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <section xml:id="sd1SectCircleArea"> + <qandaset defaultlabel="qanda" xml:id="sw1QandaCircleArea"> <title>Calculating a circle area</title> - <qandaset defaultlabel="qanda" xml:id="sw1QandaCircleArea"> - <qandadiv> - <qandaentry> - <question> - <para>The area <inlineequation> - <m:math display="inline"> + <qandadiv> + <qandaentry> + <question> + <para>The area <inlineequation> + <m:math display="inline"> + <m:mi>a</m:mi> + </m:math> + </inlineequation> of a given circle having radius <inlineequation> + <m:math display="inline"> + <m:mi>r</m:mi> + </m:math> + </inlineequation> is being obtained by <inlineequation> + <m:math display="inline"> + <m:mrow> <m:mi>a</m:mi> - </m:math> - </inlineequation> of a given circle having radius - <inlineequation> - <m:math display="inline"> - <m:mi>r</m:mi> - </m:math> - </inlineequation> is being obtained by <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mi>a</m:mi> - <m:mo>=</m:mo> + <m:mo>=</m:mo> - <m:mrow> - <m:mi>Ï€</m:mi> + <m:mrow> + <m:mi>Ï€</m:mi> - <m:mo>×</m:mo> + <m:mo>×</m:mo> - <m:msup> - <m:mi>r</m:mi> + <m:msup> + <m:mi>r</m:mi> - <m:mi>2</m:mi> - </m:msup> - </m:mrow> + <m:mi>2</m:mi> + </m:msup> </m:mrow> - </m:math> - </inlineequation>. Complete the following code to calculate - the result and write it to standard output using - <code>System.out.println(...)</code>:</para> + </m:mrow> + </m:math> + </inlineequation>. Complete the following code to calculate the result and write it to standard output using <code>System.out.println(...)</code>:</para> - <programlisting language="java">public static void main(String[] args) { + <programlisting language="java">public static void main(String[] args) { double radius = 2.31; // A circle having a radius (given e.g. in mm). double pi = 3.1415926; // Constant relating a circle's radius, @@ -2101,20 +1890,18 @@ System.out.println("Character: " + arabicChar);</programlisting> // TODO: Write the circle's area to standard output }</programlisting> - <tip> - <para>You may want to read the <link - xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc1_html_4">overview - section</link> on statements.</para> - </tip> - </question> + <tip> + <para>You may want to read the <link xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc1_html_4">overview section</link> on statements.</para> + </tip> + </question> - <answer> - <glosslist> - <glossentry> - <glossterm>Using a mere expression</glossterm> + <answer> + <glosslist> + <glossentry> + <glossterm>Using a mere expression</glossterm> - <glossdef> - <programlisting language="java">public static void main(String[] args) { + <glossdef> + <programlisting language="java">public static void main(String[] args) { double radius = 2.31; // A circle having a radius (given e.g. in mm). double pi = 3.1415926; // Constant relating a circle's radius, @@ -2122,19 +1909,16 @@ System.out.println("Character: " + arabicChar);</programlisting> System.out.println(pi * radius * radius); }</programlisting> - </glossdef> - </glossentry> + </glossdef> + </glossentry> - <glossentry> - <glossterm>Using a variable</glossterm> + <glossentry> + <glossterm>Using a variable</glossterm> - <glossdef> - <para>Instead of immediately using the expression as an - argument to <code>System.out.println(...)</code> we may - assign its value to a variable prior to output - creation:</para> + <glossdef> + <para>Instead of immediately using the expression as an argument to <code>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) { + <programlisting language="java">public static void main(String[] args) { double radius = 2.31; // A circle having a radius (given e.g. in mm). double pi = 3.1415926; // Constant relating a circle's radius, @@ -2143,27 +1927,23 @@ System.out.println("Character: " + arabicChar);</programlisting> double area = pi * radius * radius; System.out.println(area); }</programlisting> - </glossdef> - </glossentry> - </glosslist> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + </glossdef> + </glossentry> + </glosslist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <section xml:id="sd1SectTemperatureConvert"> + <qandaset defaultlabel="qanda" xml:id="sd1QandaTemperatureConvert"> <title>Converting temperature values</title> - <qandaset defaultlabel="qanda" xml:id="sd1QandaTemperatureConvert"> - <qandadiv> - <qandaentry> - <question> - <para>Write an application converting temperature values being - represented as degree centigrade to kelvin and - Fahrenheit:</para> + <qandadiv> + <qandaentry> + <question> + <para>Write an application converting temperature values being represented as degree centigrade to kelvin and Fahrenheit:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { double temperatureCelsius = 23.2; @@ -2173,10 +1953,10 @@ System.out.println("Character: " + arabicChar);</programlisting> System.out.println("Kelvin: " + temperatureKelvin); System.out.println("Fahrenheit: " + temperatureFahrenheit); }</programlisting> - </question> + </question> - <answer> - <programlisting language="java"> public static void main(String[] args) { + <answer> + <programlisting language="java"> public static void main(String[] args) { double temperatureCelsius = 23.2; @@ -2188,27 +1968,24 @@ System.out.println("Character: " + arabicChar);</programlisting> System.out.println("Kelvin: " + temperatureKelvin); System.out.println("Fahrenheit: " + temperatureFahrenheit); }</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <section xml:id="sd1SectTimeUnits"> + <qandaset defaultlabel="qanda" xml:id="sd1QandaTimeUnits"> <title>Time unit conversion</title> - <qandaset defaultlabel="qanda" xml:id="sd1QandaTimeUnits"> - <qandadiv> - <qandaentry> - <question> - <para>This exercise consists of two parts:</para> + <qandadiv> + <qandaentry> + <question> + <para>This exercise consists of two parts:</para> - <orderedlist> - <listitem> - <para>Write an application converting a (seconds, minutes - ,hours) time specification to seconds:</para> + <orderedlist> + <listitem> + <para>Write an application converting a (seconds, minutes ,hours) time specification to seconds:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { final int seconds = 31, @@ -2220,17 +1997,15 @@ System.out.println("Character: " + arabicChar);</programlisting> System.out.println("Time in seconds:" + timeInSeconds); }</programlisting> - <para>The expected output reads:</para> + <para>The expected output reads:</para> - <screen>Time in seconds:15391</screen> - </listitem> + <screen>Time in seconds:15391</screen> + </listitem> - <listitem> - <para>Reverse the previous part of this exercise: Convert - a time specification in seconds to (seconds, minutes - ,hours) as in:</para> + <listitem> + <para>Reverse the previous part of this exercise: Convert a time specification in seconds to (seconds, minutes ,hours) as in:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { final int timeInSeconds = 15391; @@ -2241,28 +2016,25 @@ System.out.println("Character: " + arabicChar);</programlisting> System.out.println("Seconds:" + seconds); }</programlisting> - <para>The expected output reads:</para> + <para>The expected output reads:</para> - <screen>Hours:4 + <screen>Hours:4 Minutes:16 Seconds:31</screen> - <tip> - <para>Consider the <link - xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html">remainder - operator <quote>%</quote></link> (modulo - operator).</para> - </tip> - </listitem> - </orderedlist> - </question> + <tip> + <para>Consider the <link xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html">remainder operator <quote>%</quote></link> (modulo operator).</para> + </tip> + </listitem> + </orderedlist> + </question> - <answer> - <orderedlist> - <listitem> - <para>A straightforward solution reads:</para> + <answer> + <orderedlist> + <listitem> + <para>A straightforward solution reads:</para> - <para><programlisting language="java"> public static void main(String[] args) { + <para><programlisting language="java"> public static void main(String[] args) { final int seconds = 31, @@ -2273,10 +2045,10 @@ Seconds:31</screen> System.out.println("Time in seconds:" + timeInSeconds); }</programlisting></para> - </listitem> + </listitem> - <listitem> - <programlisting language="java"> public static void main(String[] args) { + <listitem> + <programlisting language="java"> public static void main(String[] args) { final int timeInSeconds = 15391; @@ -2290,622 +2062,545 @@ Seconds:31</screen> System.out.println("Minutes:" + minutes); System.out.println("Seconds:" + seconds); }</programlisting> - </listitem> - </orderedlist> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + </listitem> + </orderedlist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <section xml:id="sd1_sect_typeAssignCompat"> + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_typeAssignCompat"> <title>Assignment and type safety</title> - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_typeAssignCompat"> - <qandadiv> - <qandaentry> - <question> - <para>We consider:</para> + <qandadiv> + <qandaentry> + <question> + <para>We consider:</para> - <programlisting language="java">int i = -21; + <programlisting language="java">int i = -21; double d = i;</programlisting> - <para>This snippet shows perfectly correct <xref - linkend="glo_Java"/> code. However <xref - linkend="sd1_fig_typeSafety"/> suggests we should not be - allowed to assign an <code>int</code> to a <code>double</code> - value due to type safety.</para> + <para>This snippet shows perfectly correct <xref linkend="glo_Java"/> code. However <xref linkend="sd1_fig_typeSafety"/> suggests we should not be allowed to assign an <code>int</code> to a <code>double</code> value due to type safety.</para> - <para>Even worse: Swapping types yields a compile time - error:</para> + <para>Even worse: Swapping types yields a compile time error:</para> - <programlisting language="java">double d = 2.4; + <programlisting language="java">double d = 2.4; int i = d; // Error: Incompatible types</programlisting> - <para>Why is this? Make an educated guess.</para> - </question> - - <answer> - <para>The first snippet compiles by using the widening - conversion: When assigning <code>d = i</code> the <xref - linkend="glo_Java"/> compiler implicitly converts the - <code>int</code> into a <code>double</code> value.</para> - - <para>Turning a double into an int is more cumbersome: The - expression <code>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>int</code>'s maximum value is <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <para>Why is this? Make an educated guess.</para> + </question> - <m:mi>31</m:mi> - </m:msup> + <answer> + <para>The first snippet compiles by using the widening conversion: When assigning <code>d = i</code> the <xref linkend="glo_Java"/> compiler implicitly converts the <code>int</code> into a <code>double</code> value.</para> - <m:mo>-</m:mo> + <para>Turning a double into an int is more cumbersome: The expression <code>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>int</code>'s maximum value is <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation>exceeding the former <code>double</code> - value.</para> + <m:mi>31</m:mi> + </m:msup> + + <m:mo>-</m:mo> - <para>Conclusion: <xref linkend="glo_Java"/> disallows double - to int assignments unless using a so called cast (explicit - type conversion):</para> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation>exceeding the former <code>double</code> value.</para> - <programlisting language="java">double d = 2.4; + <para>Conclusion: <xref linkend="glo_Java"/> disallows double to int assignments unless using a so called cast (explicit type conversion):</para> + + <programlisting language="java">double d = 2.4; int i = (int) d; // Explicit cast double to int</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <section xml:id="sd1SectLong2Float"> + <qandaset defaultlabel="qanda" xml:id="sd1QandaLong2Float"> <title>Reducing <code>long</code> to <code>int</code></title> - <qandaset defaultlabel="qanda" xml:id="sd1QandaLong2Float"> - <qandadiv> - <qandaentry> - <question> - <para>Joe programmer intends to map positive <code>long</code> - values to <code>int</code> values. This requires scaling down - <quote>half</quote> the <code>long</code> data type's range - <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>[</m:mo> + <qandadiv> + <qandaentry> + <question> + <para>Joe programmer intends to map positive <code>long</code> values to <code>int</code> values. This requires scaling down <quote>half</quote> the <code>long</code> data type's range <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>[</m:mo> - <m:mrow> - <m:mn>0</m:mn> + <m:mrow> + <m:mn>0</m:mn> - <m:mo>,</m:mo> + <m:mo>,</m:mo> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <m:mi>63</m:mi> - </m:msup> + <m:mi>63</m:mi> + </m:msup> - <m:mo>-</m:mo> + <m:mo>-</m:mo> - <m:mi>1</m:mi> - </m:mrow> + <m:mi>1</m:mi> </m:mrow> - - <m:mo>]</m:mo> </m:mrow> - </m:math> - </inlineequation> to the <code>int</code>'s range of - <inlineequation> - <m:math display="inline"> + + <m:mo>]</m:mo> + </m:mrow> + </m:math> + </inlineequation> to the <code>int</code>'s range of <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>[</m:mo> + <m:mrow> - <m:mo>[</m:mo> + <m:mn>0</m:mn> + + <m:mo>,</m:mo> <m:mrow> - <m:mn>0</m:mn> + <m:msup> + <m:mi>2</m:mi> - <m:mo>,</m:mo> + <m:mi>31</m:mi> + </m:msup> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <m:mo>-</m:mo> - <m:mi>31</m:mi> - </m:msup> + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> - <m:mo>-</m:mo> + <m:mo>]</m:mo> + </m:mrow> + </m:math> + </inlineequation>:</para> - <m:mi>1</m:mi> - </m:mrow> - </m:mrow> + <informaltable border="1"> + <colgroup width="25%"/> - <m:mo>]</m:mo> - </m:mrow> - </m:math> - </inlineequation>:</para> + <colgroup width="30%"/> - <informaltable border="1"> - <colgroup width="25%"/> + <colgroup width="15%"/> - <colgroup width="30%"/> + <colgroup width="30%"/> - <colgroup width="15%"/> + <tr> + <th colspan="2">From</th> + + <th colspan="2">To</th> + </tr> + + <tr> + <th><code>long</code></th> + + <th><code>remark</code></th> - <colgroup width="30%"/> + <th><code>int</code></th> - <tr> - <th colspan="2">From</th> + <th>remark</th> + </tr> - <th colspan="2">To</th> - </tr> + <tr> + <td align="right">0</td> - <tr> - <th><code>long</code></th> + <td align="right"/> - <th><code>remark</code></th> + <td align="right">0</td> - <th><code>int</code></th> + <td align="right"/> + </tr> - <th>remark</th> - </tr> + <tr> + <td align="right">1</td> - <tr> - <td align="right">0</td> + <td align="right"/> - <td align="right"/> + <td align="right">0</td> - <td align="right">0</td> + <td align="right"/> + </tr> - <td align="right"/> - </tr> + <tr> + <td align="center" colspan="2">...</td> - <tr> - <td align="right">1</td> + <td align="right">0</td> - <td align="right"/> + <td align="center">...</td> + </tr> - <td align="right">0</td> + <tr> + <td align="right">4294967295</td> - <td align="right"/> - </tr> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <tr> - <td align="center" colspan="2">...</td> + <m:mi>32</m:mi> + </m:msup> - <td align="right">0</td> + <m:mo>-</m:mo> - <td align="center">...</td> - </tr> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation></td> - <tr> - <td align="right">4294967295</td> + <td align="right">0</td> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <td align="right"/> + </tr> - <m:mi>32</m:mi> - </m:msup> + <tr> + <td align="right">4294967296</td> - <m:mo>-</m:mo> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:msup> + <m:mi>2</m:mi> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation></td> + <m:mi>32</m:mi> + </m:msup> + </m:math> + </inlineequation></td> - <td align="right">0</td> + <td align="right">1</td> - <td align="right"/> - </tr> + <td align="right"/> + </tr> - <tr> - <td align="right">4294967296</td> + <tr> + <td align="right">4294967297</td> - <td align="right"><inlineequation> - <m:math display="inline"> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:mrow> <m:msup> <m:mi>2</m:mi> <m:mi>32</m:mi> </m:msup> - </m:math> - </inlineequation></td> - - <td align="right">1</td> - - <td align="right"/> - </tr> - <tr> - <td align="right">4294967297</td> + <m:mo>+</m:mo> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> - - <m:mi>32</m:mi> - </m:msup> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation></td> - <m:mo>+</m:mo> + <td align="right">1</td> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation></td> + <td align="right"/> + </tr> - <td align="right">1</td> + <tr> + <td align="center" colspan="4">...</td> + </tr> - <td align="right"/> - </tr> + <tr> + <td align="right">9223372036854775806</td> - <tr> - <td align="center" colspan="4">...</td> - </tr> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <tr> - <td align="right">9223372036854775806</td> + <m:mi>63</m:mi> + </m:msup> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <m:mo>-</m:mo> - <m:mi>63</m:mi> - </m:msup> + <m:mi>2</m:mi> + </m:mrow> + </m:math> + </inlineequation> or <code xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE"><varname xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></code> - 1</td> - <m:mo>-</m:mo> + <td align="right">2147483647</td> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> <m:mi>2</m:mi> - </m:mrow> - </m:math> - </inlineequation> or <code - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE"><varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></code> - - 1</td> - <td align="right">2147483647</td> + <m:mi>31</m:mi> + </m:msup> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <m:mo>-</m:mo> - <m:mi>31</m:mi> - </m:msup> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation> or <varname xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> + </tr> - <m:mo>-</m:mo> + <tr> + <td align="right">9223372036854775807</td> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation> or <varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> - </tr> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> + + <m:mi>63</m:mi> + </m:msup> - <tr> - <td align="right">9223372036854775807</td> + <m:mo>-</m:mo> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation> or <varname xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></td> - <m:mi>63</m:mi> - </m:msup> + <td align="right">2147483647</td> - <m:mo>-</m:mo> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation> or <varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></td> + <m:mi>31</m:mi> + </m:msup> - <td align="right">2147483647</td> + <m:mo>-</m:mo> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation> or <varname xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> + </tr> + </informaltable> - <m:mi>31</m:mi> - </m:msup> + <para>Joe's idea is dividing <code>long</code> values by <inlineequation> + <m:math display="inline"> + <m:msup> + <m:mi>2</m:mi> - <m:mo>-</m:mo> + <m:mi>32</m:mi> + </m:msup> + </m:math> + </inlineequation>. As a result a long value of <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation> or <varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> - </tr> - </informaltable> - - <para>Joe's idea is dividing <code>long</code> values by - <inlineequation> - <m:math display="inline"> <m:msup> <m:mi>2</m:mi> - <m:mi>32</m:mi> + <m:mi>63</m:mi> </m:msup> - </m:math> - </inlineequation>. As a result a long value of - <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>-</m:mo> - - <m:msup> - <m:mi>2</m:mi> - - <m:mi>63</m:mi> - </m:msup> - </m:mrow> - </m:math> - </inlineequation> will be reduced to the intended value of - <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>-</m:mo> - - <m:msup> - <m:mi>2</m:mi> + </m:mrow> + </m:math> + </inlineequation> will be reduced to the intended value of <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> - <m:mi>31</m:mi> - </m:msup> - </m:mrow> - </m:math> - </inlineequation>. Since <inlineequation> - <m:math display="inline"> <m:msup> <m:mi>2</m:mi> - <m:mi>32</m:mi> + <m:mi>31</m:mi> </m:msup> - </m:math> - </inlineequation> seems to be equal to <code>2 * (<link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</link> - + 1))</code> (why?) Joe's first attempt reads:</para> + </m:mrow> + </m:math> + </inlineequation>. Since <inlineequation> + <m:math display="inline"> + <m:msup> + <m:mi>2</m:mi> + + <m:mi>32</m:mi> + </m:msup> + </m:math> + </inlineequation> seems to be equal to <code>2 * (<link xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</link> + 1))</code> (why?) Joe's first attempt reads:</para> - <programlisting language="java" linenumbering="numbered" - startinglinenumber="26">final long longValue = 2147483648L; + <programlisting language="java" linenumbering="numbered" startinglinenumber="26">final long longValue = 2147483648L; final int reducedValue = (int) (longValue / (2 * (Integer.MAX_VALUE + 1))); System.out.println(reducedValue);</programlisting> - <para>Unfortunately the results are not promising. This code - merely results in a runtime error:</para> + <para>Unfortunately the results are not promising. This code merely results in a runtime error:</para> - <screen>/usr/lib/jvm/java-8-oracle/bin/java ... + <screen>/usr/lib/jvm/java-8-oracle/bin/java ... Exception in thread "main" java.lang.ArithmeticException: / by zero at qq.App.main(App.java:27) Process finished with exit code 1</screen> - <para>Explain the underlying problem and correct Joe's - error.</para> + <para>Explain the underlying problem and correct Joe's error.</para> - <tip> - <para>It may be helpful thinking of a smaller example - before. Consider hypothetic integer types - <quote>TinyLong</quote> and <quote>TinyInt</quote> requiring - 4 and 2 bits respectively.</para> - </tip> - </question> + <tip> + <para>It may be helpful thinking of a smaller example before. Consider hypothetic integer types <quote>TinyLong</quote> and <quote>TinyInt</quote> requiring 4 and 2 bits respectively.</para> + </tip> + </question> - <answer> - <para>As a prerequisite we represent the scaling of a - hypothetic integer type <quote>TinyLong</quote> to a smaller - type <quote>TinyInt</quote> of 4 and 2 bits - respectively:</para> + <answer> + <para>As a prerequisite we represent the scaling of a hypothetic integer type <quote>TinyLong</quote> to a smaller type <quote>TinyInt</quote> of 4 and 2 bits respectively:</para> - <informaltable border="1"> - <colgroup width="20%"/> + <informaltable border="1"> + <colgroup width="20%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <tr> - <td><quote>TinyLong</quote>, n = 4</td> + <tr> + <td><quote>TinyLong</quote>, n = 4</td> - <td>-8</td> + <td>-8</td> - <td>-7</td> + <td>-7</td> - <td>-6</td> + <td>-6</td> - <td>-5</td> + <td>-5</td> - <td>-4</td> + <td>-4</td> - <td>-3</td> + <td>-3</td> - <td>-2</td> + <td>-2</td> - <td>-1</td> + <td>-1</td> - <td>0</td> + <td>0</td> - <td>1</td> + <td>1</td> - <td>2</td> + <td>2</td> - <td>3</td> + <td>3</td> - <td>4</td> + <td>4</td> - <td>5</td> + <td>5</td> - <td>6</td> + <td>6</td> - <td>7</td> - </tr> + <td>7</td> + </tr> - <tr> - <td><quote>TinyInt</quote>, n = 2</td> + <tr> + <td><quote>TinyInt</quote>, n = 2</td> - <td colspan="4">-2</td> + <td colspan="4">-2</td> - <td colspan="4">-1</td> + <td colspan="4">-1</td> - <td colspan="4">0</td> + <td colspan="4">0</td> - <td colspan="4">1</td> - </tr> - </informaltable> + <td colspan="4">1</td> + </tr> + </informaltable> - <para>Joe's assumption for these particular values implies - dividing <quote>TinyLong</quote> values by <inlineequation> - <m:math display="inline"> - <m:msup> - <m:mi>2</m:mi> + <para>Joe's assumption for these particular values implies dividing <quote>TinyLong</quote> values by <inlineequation> + <m:math display="inline"> + <m:msup> + <m:mi>2</m:mi> - <m:mi>2</m:mi> - </m:msup> - </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>longValue / (2 * (Integer.MAX_VALUE + 1))</code> - ?</para> + <m:mi>2</m:mi> + </m:msup> + </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>longValue / (2 * (Integer.MAX_VALUE + 1))</code> ?</para> - <para>Unfortunately Joe's implementation is seriously flawed - for even two reasons:</para> + <para>Unfortunately Joe's implementation is seriously flawed for even two reasons:</para> - <orderedlist> - <listitem> - <para>The constant <code>Integer.MAX_VALUE</code> already - suggests we will not be able to increase its value while - staying as an <code>int</code>. The expression - <code>Integer.MAX_VALUE + 1</code> will be evaluated using - <code>int</code> rather than <code>long</code> arithmetic - thus returning <code>Integer.MIN_VALUE</code> (negative!) - due to an arithmetic overflow:</para> - - <programlisting language="java"> 01111111_11111111_11111111_11111111 + <orderedlist> + <listitem> + <para>The constant <code>Integer.MAX_VALUE</code> already suggests we will not be able to increase its value while staying as an <code>int</code>. The expression <code>Integer.MAX_VALUE + 1</code> will be evaluated using <code>int</code> rather than <code>long</code> arithmetic thus returning <code>Integer.MIN_VALUE</code> (negative!) due to an arithmetic overflow:</para> + + <programlisting language="java"> 01111111_11111111_11111111_11111111 + 00000000_00000000_00000000_00000001 _____________________________________ 10000000_00000000_00000000_00000000</programlisting> - <para>The expression <code>2 * (Integer.MAX_VALUE + - 1)</code> then gives rise to a second overflow - error:</para> + <para>The expression <code>2 * (Integer.MAX_VALUE + 1)</code> then gives rise to a second overflow error:</para> - <programlisting language="java"> 10000000_00000000_00000000_00000000 + <programlisting language="java"> 10000000_00000000_00000000_00000000 + 10000000_00000000_00000000_00000000 _____________________________________ 00000000_00000000_00000000_00000000</programlisting> - </listitem> - </orderedlist> - - <para>Both errors combined (surprisingly) result in a value of - <code>0</code> explaining the <quote>division by zero</quote> - error message. There are two possible solutions:</para> - - <glosslist> - <glossentry> - <glossterm><code>(int) (longValue / (2L * - (Integer.MAX_VALUE + 1L)))</code></glossterm> - - <glossdef> - <para>Introducing <code>2L</code> or <code>1L</code> - (one is sufficient) in favour of simply using 2 and 1 - turns both addition and multiplication into operations - involving at least one long argument. Thus for both - operations the <xref linkend="glo_Java"/> runtime will - use <code>long</code> arithmetic returning the desired - <quote>reducing</quote> factor of <inlineequation> - <m:math display="inline"> - <m:msup> - <m:mi>2</m:mi> - - <m:mi>32</m:mi> - </m:msup> - </m:math> - </inlineequation> of type <code>long</code>.</para> - </glossdef> - </glossentry> - - <glossentry> - <glossterm><code>(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>longValue / 2 ...</code> Since the variable - <code>longValue</code> is of type <code>long</code> - the expression <code>longValue / 2</code> will be - evaluated by the <xref linkend="glo_Java"/> runtime - using <code>long</code> arithmetics. The result will - subsequently be multiplied with - <code>Integer.MAX_VALUE + 1L</code> again using - <code>long</code> arithmetic.</para> - </note> - </glossdef> - </glossentry> - </glosslist> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + </listitem> + </orderedlist> + + <para>Both errors combined (surprisingly) result in a value of <code>0</code> explaining the <quote>division by zero</quote> error message. There are two possible solutions:</para> + + <glosslist> + <glossentry> + <glossterm><code>(int) (longValue / (2L * (Integer.MAX_VALUE + 1L)))</code></glossterm> + + <glossdef> + <para>Introducing <code>2L</code> or <code>1L</code> (one is sufficient) in favour of simply using 2 and 1 turns both addition and multiplication into operations involving at least one long argument. Thus for both operations the <xref linkend="glo_Java"/> runtime will use <code>long</code> arithmetic returning the desired <quote>reducing</quote> factor of <inlineequation> + <m:math display="inline"> + <m:msup> + <m:mi>2</m:mi> + + <m:mi>32</m:mi> + </m:msup> + </m:math> + </inlineequation> of type <code>long</code>.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm><code>(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>longValue / 2 ...</code> Since the variable <code>longValue</code> is of type <code>long</code> the expression <code>longValue / 2</code> will be evaluated by the <xref linkend="glo_Java"/> runtime using <code>long</code> arithmetics. The result will subsequently be multiplied with <code>Integer.MAX_VALUE + 1L</code> again using <code>long</code> arithmetic.</para> + </note> + </glossdef> + </glossentry> + </glosslist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <section xml:id="sd1SectFloatPrecisionProblem"> + <qandaset defaultlabel="qanda" xml:id="sd1QandaFloatPrecisionProblem"> <title>Representational float and double miracles</title> - <qandaset defaultlabel="qanda" xml:id="sd1QandaFloatPrecisionProblem"> - <qandadiv> - <qandaentry> - <question> - <para>Consider and execute the following code snippet:</para> + <qandadiv> + <qandaentry> + <question> + <para>Consider and execute the following code snippet:</para> - <programlisting language="java">public static void main(String[] args) { + <programlisting language="java">public static void main(String[] args) { final double a = 0.7; final double b = 0.9; final double x = a + 0.1; @@ -2913,82 +2608,67 @@ _____________________________________ System.out.println(x == y); }</programlisting> - <para>Explain the result and supply a possible - solution.</para> - </question> + <para>Explain the result and supply a possible solution.</para> + </question> - <answer> - <para>The expression <code>x == y</code> evaluates to - <code>false</code>. This surprising result is due to limited - precision regarding both <code>float</code> and - <code>double</code> IEEE representations: A given value will - be approximated as close as possible.</para> + <answer> + <para>The expression <code>x == y</code> evaluates to <code>false</code>. This surprising result is due to limited precision regarding both <code>float</code> and <code>double</code> IEEE representations: A given value will be approximated as close as possible.</para> - <para>Adding <code>System.out.println(x - y)</code> yields a - value of -1.1102230246251565E-16 denoting the representational - deviation of <code>x</code> and <code>y</code>.</para> + <para>Adding <code>System.out.println(x - y)</code> yields a value of -1.1102230246251565E-16 denoting the representational deviation of <code>x</code> and <code>y</code>.</para> - <para>So we may compare <code>float</code> and - <code>double</code> values by providing a representational - error limit:</para> + <para>So we may compare <code>float</code> and <code>double</code> values by providing a representational error limit:</para> - <programlisting language="java">final double a = 0.7; + <programlisting language="java">final double a = 0.7; final double b = 0.9; final double x = a + 0.1; final double y = b - 0.1; -System.out.println(<link - xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#abs-double-">Math.abs</link>(x - y) < 1.E-14);</programlisting> +System.out.println(<link xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#abs-double-">Math.abs</link>(x - y) < 1.E-14);</programlisting> - <para>This implements the boolean expression <inlineequation> - <m:math display="inline"> + <para>This implements the boolean expression <inlineequation> + <m:math display="inline"> + <m:mrow> <m:mrow> - <m:mrow> - <m:mo>|</m:mo> - - <m:mrow> - <m:mi>x</m:mi> + <m:mo>|</m:mo> - <m:mo>-</m:mo> + <m:mrow> + <m:mi>x</m:mi> - <m:mi>y</m:mi> - </m:mrow> + <m:mo>-</m:mo> - <m:mo>|</m:mo> + <m:mi>y</m:mi> </m:mrow> - <m:mo><</m:mo> + <m:mo>|</m:mo> + </m:mrow> - <m:msup> - <m:mi>10</m:mi> + <m:mo><</m:mo> - <m:mrow> - <m:mo>-</m:mo> + <m:msup> + <m:mi>10</m:mi> - <m:mi>14</m:mi> - </m:mrow> - </m:msup> - </m:mrow> - </m:math> - </inlineequation>.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <m:mrow> + <m:mo>-</m:mo> - <section xml:id="sd1SectInterestSimple"> + <m:mi>14</m:mi> + </m:mrow> + </m:msup> + </m:mrow> + </m:math> + </inlineequation>.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaInterestSimple"> <title>Interest calculation</title> - <qandaset defaultlabel="qanda" xml:id="sd1QandaInterestSimple"> - <qandadiv> - <qandaentry> - <question> - <para>We want to calculate the compounded interest starting - from an initial capital, a given annual interest rate and a - duration of three years. Consider the following code - fragment:</para> + <qandadiv> + <qandaentry> + <question> + <para>We want to calculate the compounded interest starting from an initial capital, a given annual interest rate and a duration of three years. Consider the following code fragment:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { final double initialCapital = 223.12; final double interestRate = 1.5; @@ -3001,43 +2681,39 @@ System.out.println(<link System.out.println("Capital after three years:" + ...); }</programlisting> - <para>The expected output is:</para> + <para>The expected output is:</para> - <screen>Initial capital:223.12 + <screen>Initial capital:223.12 Annual interest rate:1.5 Capital after three years:233.31175902999993</screen> - <tip> - <para>In case you are unsure read about calculating the - compounded interest.</para> - </tip> - </question> + <tip> + <para>In case you are unsure read about calculating the compounded interest.</para> + </tip> + </question> - <answer> - <para>We obtain the compounded interest by multiplying the - initial capital by <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mn>1</m:mn> + <answer> + <para>We obtain the compounded interest by multiplying the initial capital by <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mn>1</m:mn> - <m:mo>+</m:mo> + <m:mo>+</m:mo> - <m:mrow> - <m:mi>r</m:mi> + <m:mrow> + <m:mi>r</m:mi> - <m:mo>/</m:mo> + <m:mo>/</m:mo> - <m:mn>100</m:mn> - </m:mrow> + <m:mn>100</m:mn> </m:mrow> - </m:math> - </inlineequation> for each year where r represents the given - interest rate.</para> + </m:mrow> + </m:math> + </inlineequation> for each year where r represents the given interest rate.</para> - <para>Since we have not yet introduced loops this - multiplication has to be repeated three times:</para> + <para>Since we have not yet introduced loops this multiplication has to be repeated three times:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { final double initialCapital = 223.12; final double interestRate = 1.5; @@ -3055,10 +2731,9 @@ Capital after three years:233.31175902999993</screen> System.out.println("Capital after three years:" + capitalAtThreeYears); }</programlisting> - <para>We might as well use a single arithmetic expression to - achieve the same result:</para> + <para>We might as well use a single arithmetic expression to achieve the same result:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { final double initialCapital = 223.12; final double interestRate = 1.5; @@ -3073,225 +2748,99 @@ Capital after three years:233.31175902999993</screen> System.out.println("Capital after three years:" + capitalAtThreeYears); }</programlisting> - <para>In <xref linkend="sd1InterestCalculator"/> we will - present a more elaborate solution based on loops and class - methods.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <para>In <xref linkend="sd1InterestCalculator"/> we will present a more elaborate solution based on loops and class methods.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <section xml:id="sd1ExpressionStrangeThings"> + <qandaset defaultlabel="qanda" xml:id="sw1QandaByteOverflow"> <title>Strange things happen</title> - <qandaset defaultlabel="qanda" xml:id="sw1QandaByteOverflow"> - <title>Byte integer arithmetics</title> - - <qandadiv> - <qandaentry> - <question> - <para>Consider the following code snippet:</para> + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code snippet:</para> - <programlisting language="java">byte a = 127; + <programlisting language="java">byte a = 127; System.out.println("value=" + a); a++; System.out.println("New value=" + a); </programlisting> - <para>This will produce the following output:</para> + <para>This will produce the following output:</para> - <screen>value=127 + <screen>value=127 New value=-128</screen> - <para>Explain this strange behaviour.</para> + <para>Explain this strange behaviour.</para> - <para>Moreover you'll find the following code snippet yields a - compile time error:</para> + <para>Moreover you'll find the following code snippet yields a compile time error:</para> - <programlisting language="java">byte a = 127; + <programlisting language="java">byte a = 127; 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>Explain this error's cause.</para> + <para>Explain this error's cause.</para> - <tip> - <para>You may want to read the <link - xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc1_html_4">overview - section</link> on statements.</para> - </tip> - </question> + <tip> + <para>You may want to read the <link xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc1_html_4">overview section</link> on statements.</para> + </tip> + </question> - <answer> - <para>A byte variable ranges from -128 to +127. Thus - incrementing 127 by 1 yields -128 and thus an overflow - error.</para> + <answer> + <para>A byte variable ranges from -128 to +127. Thus incrementing 127 by 1 yields -128 and thus an overflow error.</para> - <para>Since <xref linkend="glo_Java"/> uses <link - xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representation we have:</para> + <para>Since <xref linkend="glo_Java"/> uses <link xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's complement</link> representation we have:</para> - <informaltable border="1"> - <tr> - <td><code> 01111111</code></td> + <informaltable border="1"> + <tr> + <td><code> 01111111</code></td> - <td><code> 127</code></td> - </tr> + <td><code> 127</code></td> + </tr> - <tr> - <td><code>+00000001</code></td> + <tr> + <td><code>+00000001</code></td> - <td><code> +1</code></td> - </tr> + <td><code> +1</code></td> + </tr> - <tr> - <td><code>=10000000</code></td> + <tr> + <td><code>=10000000</code></td> - <td><code>=-128</code></td> - </tr> - </informaltable> + <td><code>=-128</code></td> + </tr> + </informaltable> - <para>On machine level the above calculation is just an - ordinary addition.</para> + <para>On machine level the above calculation is just an ordinary addition.</para> - <para>Conclusion: Watch out when doing (integer) - arithmetic!</para> + <para>Conclusion: Watch out when doing (integer) arithmetic!</para> - <para>The compile time error is due to the definition of the - <quote>+</quote> operator in Java always returning an - <code>int</code> rather than a byte. Consider:</para> + <para>The compile time error is due to the definition of the <quote>+</quote> operator in Java always returning an <code>int</code> rather than a byte. Consider:</para> - <programlisting language="java"> byte a = 120, b = 10; + <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>int</code> value.</para> + <para>This yields the expected output of 130 and corresponds to an <code>int</code> value.</para> - <para>If the expression <code>a + b</code> was of data type - <code>byte</code> an arithmetic overflow as in the subsequent - code example would occur:</para> + <para>If the expression <code>a + b</code> was of data type <code>byte</code> an arithmetic overflow as in the subsequent code example would occur:</para> - <programlisting language="java"> byte a = 120, b = 10; + <programlisting language="java"> byte a = 120, b = 10; byte sum = (byte) (a + b); System.out.println(sum);</programlisting> - <para>The explicit type conversion (a so called type cast or - cast for short) forces the 4-byte integer into a one-byte - variable <code>sum</code> thereby loosing the original value - and returning -126 instead.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <para>The explicit type conversion (a so called type cast or cast for short) forces the 4-byte integer into a one-byte variable <code>sum</code> thereby loosing the original value and returning -126 instead.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> <section xml:id="sw1SectAssignArithmetics"> <title>Assignments and simple arithmetics</title> - - <qandaset defaultlabel="qanda" xml:id="sw1QandaGuessingResults"> - <title>Guessing results</title> - - <qandadiv> - <qandaentry> - <question> - <para>Consider the following code segment:</para> - - <programlisting language="java"> int a = 3; - a++; //Incrementing a by 1 --> a==4 - - int b = a; // TODO - - b--; // TODO - --b; // TODO - - int c = b; // TODO - - b = ++a; // TODO - int e = a++; // TODO - - a *= b; // TODO - - System.out.println("a=" + a); - System.out.println("b=" + b); - System.out.println("c=" + c); - System.out.println("e=" + e);</programlisting> - - <para>We want to guess the expected result. Copy/paste the - above code into a main() method body within your IDE of choice - (probably Eclipse). Though it is tempting to just execute this - code ant watch the results <emphasis role="bold">please - refrain from doing so!</emphasis></para> - - <para>Instead start a <quote>dry run</quote> complete the - above comments to predict the expected outcome:</para> - - <para><computeroutput>a=...</computeroutput></para> - - <para><computeroutput>b=...</computeroutput></para> - - <para><computeroutput>c=...</computeroutput></para> - - <para><computeroutput>e=...</computeroutput></para> - - <para>After finishing your guesses execute your code and start - wailing about your success rate.</para> - - <tip> - <para>Both <code>x++</code> (postfix notation) and - <code>++x</code> (infix notation) are expressions themselves - which might even be rewritten as <code>(x++)</code> and - <code>(++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>++</code> and <code>--</code>. A corresponding - expression evaluates to:</para> - - <itemizedlist> - <listitem> - <para><code>a = x++</code> yields a ==x</para> - </listitem> - - <listitem> - <para><code>a = ++x</code> yields a ==(x + 1)</para> - </listitem> - </itemizedlist> - - <para>The rest is just obeying the <quote>due - diligence</quote> rule set:</para> - - <programlisting language="java"> int a = 3; - a++; //Incrementing a by 1 --> a==4 - - int b = a; // Assigning value of a --> b==4 - - b--; // Decrementing b by 1 --> b==3 - --b; // Decrementing b by 1 --> b==2 - - int c = b; // c == 2; - - b = ++a; // Incrementing a by 1 -->a==5, then assigning to b --> b == 5 - int e = a++; // Assigning a to e --> e==5, then incrementing a --> a==6 - - a *= b; // Multiplying a with b and assigning the result to a --> a==30 - - System.out.println("a=" + a); - System.out.println("b=" + b); - System.out.println("c=" + c); - System.out.println("e=" + e);</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> </section> </section> @@ -3313,8 +2862,7 @@ System.out.println("New value=" + a);</programlisting> }</programlisting> - <para>Complete the above snippet to produce the following - output:</para> + <para>Complete the above snippet to produce the following output:</para> <screen>3 Games having 22 players each results in 66 players altogether.</screen> </question> @@ -3345,17 +2893,10 @@ System.out.println("New value=" + a);</programlisting> System.out.println("Some 'special' words."); }</programlisting> - <para>The corresponding output will be <computeroutput>Some - 'special' words.</computeroutput>. Change the above code to - replace the single quotes by double quotes producing the output - <computeroutput>Some "special" words.</computeroutput> - instead.</para> + <para>The corresponding output will be <computeroutput>Some 'special' words.</computeroutput>. Change the above code to replace the single quotes by double quotes producing the output <computeroutput>Some "special" words.</computeroutput> instead.</para> <tip> - <para>Hunt for <quote>java escape double quote</quote> and - read about <link - xlink:href="https://proquest.safaribooksonline.com/book/programming/java/9780992133047/chapter-2-language-fundamentals/toc6_html_2#readertoolbar2">character - literals</link>.</para> + <para>Hunt for <quote>java escape double quote</quote> and read about <link xlink:href="https://proquest.safaribooksonline.com/book/programming/java/9780992133047/chapter-2-language-fundamentals/toc6_html_2#readertoolbar2">character literals</link>.</para> </tip> </question> @@ -3367,8 +2908,7 @@ System.out.println("New value=" + a);</programlisting> <glossterm>Perfectly obvious</glossterm> <glossdef> - <para>Inside a string literal the string terminating - character (") may be escaped using backslashes:</para> + <para>Inside a string literal the string terminating character (") may be escaped using backslashes:</para> <programlisting language="java">System.out.println("Some \"special\" words.");</programlisting> </glossdef> @@ -3378,8 +2918,7 @@ System.out.println("New value=" + a);</programlisting> <glossterm>Even more clumsy</glossterm> <glossdef> - <para>Double quotes may be also represented by their char - (not string!) literal:</para> + <para>Double quotes may be also represented by their char (not string!) literal:</para> <programlisting language="java">System.out.println("Some " + '"' + "special" + '"' + " words.");</programlisting> </glossdef> @@ -3396,23 +2935,19 @@ System.out.println("New value=" + a);</programlisting> <qandadiv> <qandaentry> <question> - <para xml:id="sw1QandaStringCodingBatExtra">Solve the following - external exercises:</para> + <para xml:id="sw1QandaStringCodingBatExtra">Solve the following external exercises:</para> <itemizedlist> <listitem> - <para - xlink:href="http://codingbat.com/prob/p171896">helloName</para> + <para xlink:href="http://codingbat.com/prob/p171896">helloName</para> </listitem> <listitem> - <para - xlink:href="http://codingbat.com/prob/p161056">makeAbba</para> + <para xlink:href="http://codingbat.com/prob/p161056">makeAbba</para> </listitem> <listitem> - <para - xlink:href="http://codingbat.com/prob/p147483">makeTags</para> + <para xlink:href="http://codingbat.com/prob/p147483">makeTags</para> </listitem> </itemizedlist> </question> @@ -3425,14 +2960,12 @@ System.out.println("New value=" + a);</programlisting> <title>Using <code>final</code></title> <qandaset defaultlabel="qanda" xml:id="sw1QandaCircleAreaFinal"> - <title>Calculating the area of a circle avoiding accidental - redefinition</title> + <title>Calculating the area of a circle avoiding accidental redefinition</title> <qandadiv> <qandaentry> <question> - <para>In Exercise <xref linkend="sd1SectCircleArea"/> you - calculated a given circle's area:</para> + <para>In Exercise <xref linkend="sw1QandaCircleArea"/> you calculated a given circle's area:</para> <programlisting language="java"> public static void main(String[] args) { @@ -3444,9 +2977,7 @@ System.out.println("New value=" + a);</programlisting> System.out.println(area); }</programlisting> - <para>Though there is nothing wrong with this approach it - actually is error prone: A careless programmer may accidentally - redefine the value of <code>pi</code>:</para> + <para>Though there is nothing wrong with this approach it actually is error prone: A careless programmer may accidentally redefine the value of <code>pi</code>:</para> <programlisting language="java"> public static void main(String[] args) { @@ -3466,20 +2997,15 @@ System.out.println("New value=" + a);</programlisting> System.out.println(area); }</programlisting> - <para>Modify the original code to avoid this type of - error.</para> + <para>Modify the original code to avoid this type of error.</para> <tip> - <para>You may want to read the <quote - xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc5_html_2">Constants</quote> - section of <xref linkend="bib_Kurniawan2015"/>.</para> + <para>You may want to read the <quote xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc5_html_2">Constants</quote> section of <xref linkend="bib_Kurniawan2015"/>.</para> </tip> </question> <answer> - <para>The solution is straightforward. We add the - <code>final</code> modifier to the definition of our variable - <code>pi</code>:</para> + <para>The solution is straightforward. We add the <code>final</code> modifier to the definition of our variable <code>pi</code>:</para> <programlisting language="java" linenumbering="numbered">public static void main(String[] args) { @@ -3499,20 +3025,13 @@ System.out.println("New value=" + a);</programlisting> System.out.println(area); }</programlisting> - <para>Now our flawed assignment at line 9 will be flagged as a - compile time error:</para> + <para>Now our flawed assignment at line 9 will be flagged as a compile time error:</para> - <para><computeroutput>The final local variable pi cannot be - assigned. It must be blank and not using a compound - assignment</computeroutput></para> + <para><computeroutput>The final local variable pi cannot be assigned. It must be blank and not using a compound assignment</computeroutput></para> - <para>Note the second <code>final</code> modification in the - definition of our variable <code>area</code> to avoid erroneous - redefinitions as well.</para> + <para>Note the second <code>final</code> modification in the definition of our variable <code>area</code> to avoid erroneous redefinitions as well.</para> - <para>As a rule of thumb: Whenever you intend a variable not to - change after an initial assignment use <code>final</code> - declaring it to remain constant.</para> + <para>As a rule of thumb: Whenever you intend a variable not to change after an initial assignment use <code>final</code> declaring it to remain constant.</para> </answer> </qandaentry> </qandadiv> @@ -3538,14 +3057,11 @@ System.out.println("New value=" + a);</programlisting> }</programlisting> - <para>This will run smoothly producing the expected - output:</para> + <para>This will run smoothly producing the expected output:</para> <screen>20 + 3 + 9 = 32</screen> - <para>We now prettify our variable definitions by introducing - right aligning numbers thereby padding leading positions with - zeros:</para> + <para>We now prettify our variable definitions by introducing right aligning numbers thereby padding leading positions with zeros:</para> <programlisting language="java"> public static void main(String[] args) { @@ -3556,24 +3072,17 @@ System.out.println("New value=" + a);</programlisting> System.out.println(a + " + " + b + " + " + c + " = " + (a + b + c)); }</programlisting> - <para>The above code does not compile due to the compiler error - when defining variable <code>c</code>.</para> + <para>The above code does not compile due to the compiler error when defining variable <code>c</code>.</para> - <para>Explain the underlying cause of this error message. Why is - <code>b = 03</code> just fine in contrast to <code>c = 09</code> - ?</para> + <para>Explain the underlying cause of this error message. Why is <code>b = 03</code> just fine in contrast to <code>c = 09</code> ?</para> <tip> - <para>Re-read the section on integer literal - representations.</para> + <para>Re-read the section on integer literal representations.</para> </tip> </question> <answer> - <para>Integer literals starting with <quote>0</quote> are being - interpreted as octal representation. Since the octal system's - set of digits is {0,1,2,3,4,5,6,7} the value <quote>09</quote> - is simply not valid.</para> + <para>Integer literals starting with <quote>0</quote> are being interpreted as octal representation. Since the octal system's set of digits is {0,1,2,3,4,5,6,7} the value <quote>09</quote> is simply not valid.</para> </answer> </qandaentry> </qandadiv> @@ -3593,19 +3102,13 @@ System.out.println("New value=" + a);</programlisting> System.out.println("Value = " + a); }</programlisting> - <para>On execution we receive the output <code>Value = - 33</code>. Explain this result</para> + <para>On execution we receive the output <code>Value = 33</code>. Explain this result</para> </question> <answer> - <para>This problem is related to the previous exercise: The - integer literal 041 defines octal representation. Changing from - octal to decimal representation takes us to 4 * 8 + 1 = - 33.</para> - - <para>There are 11 types of people: Those, who know can read - binary codes, those who know what binary is and those who don't - have a clue what binary is.</para> + <para>This problem is related to the previous exercise: The integer literal 041 defines octal representation. Changing from octal to decimal representation takes us to 4 * 8 + 1 = 33.</para> + + <para>There are 11 types of people: Those, who know can read binary codes, those who know what binary is and those who don't have a clue what binary is.</para> </answer> </qandaentry> </qandadiv> diff --git a/ws/Docbook/CustomLayer/webhelp/positioning.css.patch b/ws/Docbook/CustomLayer/webhelp/positioning.css.patch index ccdd56d2125960e13ff81dd8482f7510fbdd74e1..aa0acdade1becbc4be66743fa6becfa3453b8541 100644 --- a/ws/Docbook/CustomLayer/webhelp/positioning.css.patch +++ b/ws/Docbook/CustomLayer/webhelp/positioning.css.patch @@ -1,5 +1,5 @@ ---- positioning.css.orig 2017-07-26 08:31:17.880119353 +0200 -+++ positioning.css 2017-07-25 17:39:11.215083986 +0200 +--- positioning.css.orig 2017-07-26 16:38:34.708386065 +0200 ++++ positioning.css 2017-07-26 16:35:36.955120822 +0200 @@ -1,3 +1,8 @@ +/* + * patched by: /usr/share/hdm-docbook-xsl/CustomLayer/webhelp/positioning.css.patch @@ -9,26 +9,33 @@ tr th .added { color: #E6E6FA; } tr th .changed {color: #99ff99; } div.added tr, div.added { background-color: #E6E6FA; } -@@ -328,9 +333,16 @@ +@@ -328,11 +333,23 @@ div#toolbar-left img {vertical-align: text-top;} div.note *, div.caution *, div.important *, div.tip *, div.warning * { - background: inherit !important; + width: fit-content; -+ border-radius: 10px; -+ padding: 5px; -+ background: var(--admonitionBackgroundColor) !important; color: inherit !important; - border: inherit !important; ++ /* border: inherit !important; */ ++} ++ ++div.note, div.caution, div.important, div.tip, div.warning { ++ width: fit-content; ++ background: var(--admonitionBackgroundColor) !important; ++ border-radius: 10px; ++ padding: 5px; + border-style: solid; + border-color: #AAAAAA; + border-width: 1px; -+ -+ /* border: inherit !important; */ } ++ ++ #content table thead, #content table th{ -@@ -375,3 +387,109 @@ + background: gray; + color: white; +@@ -375,3 +392,109 @@ } diff --git a/ws/Docbook/Extensions/Tdata/Components/exampleSlides.xml b/ws/Docbook/Extensions/Tdata/Components/exampleSlides.xml index a3a9fc0c7ed7f9d3b72048b40f3227151d3bd924..4ffb25428477a249a0c015f9a033743e2d94c15b 100644 --- a/ws/Docbook/Extensions/Tdata/Components/exampleSlides.xml +++ b/ws/Docbook/Extensions/Tdata/Components/exampleSlides.xml @@ -1,14 +1,5 @@ <?xml version="1.0" encoding="UTF-8"?> -<chapter version="5.1" xml:id="mixLectureSlides" - xmlns="http://docbook.org/ns/docbook" - xmlns:xlink="http://www.w3.org/1999/xlink" - xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes" - xmlns:xi="http://www.w3.org/2001/XInclude" - xmlns:trans="http://docbook.org/ns/transclusion" - xmlns:svg="http://www.w3.org/2000/svg" - xmlns:m="http://www.w3.org/1998/Math/MathML" - xmlns:html="http://www.w3.org/1999/xhtml" - xmlns:db="http://docbook.org/ns/docbook"> +<chapter version="5.1" xml:id="mixLectureSlides" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:trans="http://docbook.org/ns/transclusion" xmlns:svg="http://www.w3.org/2000/svg" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:db="http://docbook.org/ns/docbook"> <title>Mixing lecture notes and slides</title> <section annotations="slide" xml:id="classintro"> @@ -54,18 +45,15 @@ <figure xml:id="fig_ClassExample"> <title>General class syntax</title> - <programlisting language="java">public class TestImg { <co - linkends="javaExample-1" xml:id="javaExample-1-co"/> + <programlisting language="java">public class TestImg { <co linkends="javaExample-1" xml:id="javaExample-1-co"/> /** - * @throws JDOMException Corrupt content, failing to parse. <co - linkends="javaExample-2" xml:id="javaExample-2-co"/> + * @throws JDOMException Corrupt content, failing to parse. <co linkends="javaExample-2" xml:id="javaExample-2-co"/> * @throws IOException File access problem. */ @SuppressWarnings("static-method") @Test - public void testCsv() throws JDOMException, IOException { <co - linkends="javaExample-3" xml:id="javaExample-3-co"/></programlisting> + public void testCsv() throws JDOMException, IOException { <co linkends="javaExample-3" xml:id="javaExample-3-co"/></programlisting> <calloutlist> <callout arearefs="javaExample-1-co" xml:id="javaExample-1"> @@ -81,13 +69,16 @@ </callout> </calloutlist> </figure> + + <tip> + <para>You may require a <code>return</code> statement as well.</para> + </tip> </section> <section xml:id="exampleScreen"> <title>Screen</title> - <para>The output of <command>ls</command> <option>-al</option> - reads:</para> + <para>The output of <command>ls</command> <option>-al</option> reads:</para> <screen>goik@hopc Downloads> ls -al total 548 @@ -108,18 +99,15 @@ drwxr-xr-x 100 goik fb1prof 12288 Jul 13 07:43 .. <mediaobject> <imageobjectco> <areaspec otherunits="imagemap" units="other"> - <area coords="83,16,340,187" linkends="calloutSeat" - xml:id="seat"> + <area coords="83,16,340,187" linkends="calloutSeat" xml:id="seat"> <alt>Seat</alt> </area> - <area coords="511,345,550,380" linkends="calloutValve" - xml:id="valveFront"> + <area coords="511,345,550,380" linkends="calloutValve" xml:id="valveFront"> <alt>Valve</alt> </area> - <area coords="112,335,151,370" linkends="calloutValve" - xml:id="valveBack"> + <area coords="112,335,151,370" linkends="calloutValve" xml:id="valveBack"> <alt>Valve</alt> </area> </areaspec> @@ -354,10 +342,7 @@ drwxr-xr-x 100 goik fb1prof 12288 Jul 13 07:43 .. <m:mi>2</m:mi> </m:mrow> </m:math> - </inlineequation> or <code - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE"><varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></code> - - 1</td> + </inlineequation> or <code xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE"><varname xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></code> - 1</td> <td align="right">2147483647</td> @@ -375,8 +360,7 @@ drwxr-xr-x 100 goik fb1prof 12288 Jul 13 07:43 .. <m:mi>1</m:mi> </m:mrow> </m:math> - </inlineequation> or <varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> + </inlineequation> or <varname xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> </tr> <tr> @@ -396,8 +380,7 @@ drwxr-xr-x 100 goik fb1prof 12288 Jul 13 07:43 .. <m:mi>1</m:mi> </m:mrow> </m:math> - </inlineequation> or <varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></td> + </inlineequation> or <varname xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></td> <td align="right">2147483647</td> @@ -415,8 +398,7 @@ drwxr-xr-x 100 goik fb1prof 12288 Jul 13 07:43 .. <m:mi>1</m:mi> </m:mrow> </m:math> - </inlineequation> or <varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> + </inlineequation> or <varname xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> </tr> </informaltable> </tip> @@ -572,10 +554,7 @@ drwxr-xr-x 100 goik fb1prof 12288 Jul 13 07:43 .. <m:mi>2</m:mi> </m:mrow> </m:math> - </inlineequation> or <code - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE"><varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></code> - - 1</td> + </inlineequation> or <code xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE"><varname xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></code> - 1</td> <td align="right">2147483647</td> @@ -593,8 +572,7 @@ drwxr-xr-x 100 goik fb1prof 12288 Jul 13 07:43 .. <m:mi>1</m:mi> </m:mrow> </m:math> - </inlineequation> or <varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> + </inlineequation> or <varname xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> </tr> <tr> @@ -614,8 +592,7 @@ drwxr-xr-x 100 goik fb1prof 12288 Jul 13 07:43 .. <m:mi>1</m:mi> </m:mrow> </m:math> - </inlineequation> or <varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></td> + </inlineequation> or <varname xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></td> <td align="right">2147483647</td> @@ -633,8 +610,7 @@ drwxr-xr-x 100 goik fb1prof 12288 Jul 13 07:43 .. <m:mi>1</m:mi> </m:mrow> </m:math> - </inlineequation> or <varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> + </inlineequation> or <varname xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> </tr> </informaltable> </figure>