diff --git a/Doc/Sd1/Ref/LangFundament/keycard.jpg b/Doc/Sd1/Ref/LangFundament/keycard.jpg new file mode 100644 index 0000000000000000000000000000000000000000..3ca8ee13b40702f549c9903e101c3644877cbf23 Binary files /dev/null and b/Doc/Sd1/Ref/LangFundament/keycard.jpg differ diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml index 086d55408467d1d449119b5320b3653fb765d94b..03cc78bc37a055900a05164b8a2dcf0df655817b 100644 --- a/Doc/Sd1/languageFundamentals.xml +++ b/Doc/Sd1/languageFundamentals.xml @@ -1,14 +1,25 @@ <?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> @@ -40,7 +51,9 @@ </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> @@ -52,7 +65,9 @@ </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> @@ -64,7 +79,8 @@ </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> @@ -132,7 +148,9 @@ </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> @@ -144,7 +162,8 @@ </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> @@ -156,7 +175,66 @@ </mediaobject> </figure> - <para>Regarding language characters we start with one of the oldest and widespread character encoding schemes:</para> + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_hotelKeyCard"> + <title>Hotel key cards</title> + + <qandadiv> + <qandaentry> + <question> + <para>A hotel supplies the following type of cards for opening + room doors:</para> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/keycard.jpg" + scale="25"/> + </imageobject> + </mediaobject> + + <para>A customer is worried concerning the impact of loosing his + card. For security reasons the corresponding pattern can never + be issued again. Thus the hotel may eventually run out of + combinations.</para> + + <para>Discuss this argument by estimating the number of distinct + patterns.</para> + </question> + + <answer> + <para>The customer should not be too worried: Judging the image + a card probably offers 39 grid positions:</para> + + <programlisting language="none">X X X X X X + X X X X X +X X X X X X + X X X X X +X X X X X X + X X X X X +X X X X X X</programlisting> + + <para>Since each position may either contain a hole or be solid + we have <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> + + <m:mi>39</m:mi> + </m:msup> + + <m:mo>=</m:mo> + + <m:mi>549755813888</m:mi> + </m:mrow> + </m:math> + </inlineequation> distinct possibilities.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <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> @@ -168,7 +246,10 @@ </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> @@ -180,10 +261,16 @@ </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> @@ -192,10 +279,13 @@ </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> @@ -204,13 +294,19 @@ </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> @@ -222,7 +318,8 @@ </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> @@ -230,7 +327,8 @@ <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> @@ -298,11 +396,13 @@ 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> @@ -421,7 +521,11 @@ System.out.println(count + ":" + Count); // This yields "32:44"</programlisting> <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>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%"/> @@ -544,18 +648,24 @@ System.out.println(count + ":" + Count); // This yields "32:44"</programlisting> </informaltable> <tip> - <para>You may want to prepare a simple <xref linkend="glo_Java"/> program testing the above names.</para> + <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> + <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> + <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%"/> @@ -601,7 +711,8 @@ System.out.println(count + ":" + Count); // This yields "32:44"</programlisting> <td>no</td> - <td>Operators like <quote>-</quote> or <quote>+</quote> must not appear in variable names.</td> + <td>Operators like <quote>-</quote> or <quote>+</quote> must + not appear in variable names.</td> </tr> <tr> @@ -609,7 +720,8 @@ System.out.println(count + ":" + Count); // This yields "32:44"</programlisting> <td>yes</td> - <td>Best practices: Discouraged variable name, non-constant variables should start with lowercase letters.</td> + <td>Best practices: Discouraged variable name, non-constant + variables should start with lowercase letters.</td> </tr> <tr> @@ -625,7 +737,9 @@ System.out.println(count + ":" + Count); // This yields "32:44"</programlisting> <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> + <td><code>println</code> is a method of + <code>System.out.println(...)</code> but is no <xref + linkend="glo_Java"/> keyword.</td> </tr> <tr> @@ -633,7 +747,8 @@ System.out.println(count + ":" + Count); // This yields "32:44"</programlisting> <td>yes</td> - <td>Best practices: Discouraged variable name, non-constant variables should start with lowercase letters.</td> + <td>Best practices: Discouraged variable name, non-constant + variables should start with lowercase letters.</td> </tr> <tr> @@ -641,7 +756,9 @@ System.out.println(count + ":" + Count); // This yields "32:44"</programlisting> <td>yes</td> - <td>Best practices: Legal but discouraged variable name: Non-constant variables should start with lowercase letters.</td> + <td>Best practices: Legal but discouraged variable name: + Non-constant variables should start with lowercase + letters.</td> </tr> <tr> @@ -649,7 +766,8 @@ System.out.println(count + ":" + Count); // This yields "32:44"</programlisting> <td>no</td> - <td>String delimiters must not be part of an identifier.</td> + <td>String delimiters must not be part of an + identifier.</td> </tr> <tr> @@ -665,7 +783,8 @@ System.out.println(count + ":" + Count); // This yields "32:44"</programlisting> <td>yes</td> - <td>Perfectly legal Greek <orgname>Unicode</orgname> characters.</td> + <td>Perfectly legal Greek <orgname>Unicode</orgname> + characters.</td> </tr> <tr> @@ -673,7 +792,8 @@ System.out.println(count + ":" + Count); // This yields "32:44"</programlisting> <td>yes</td> - <td>Best practices: Using the <quote>$</quote> sign should be reserved for system code.</td> + <td>Best practices: Using the <quote>$</quote> sign should + be reserved for system code.</td> </tr> </informaltable> </answer> @@ -717,7 +837,8 @@ 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> @@ -736,9 +857,11 @@ 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> @@ -752,7 +875,8 @@ 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> @@ -772,11 +896,13 @@ 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> @@ -796,9 +922,11 @@ 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> @@ -812,7 +940,8 @@ 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> @@ -832,9 +961,11 @@ 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> @@ -848,7 +979,8 @@ 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> @@ -868,9 +1000,11 @@ 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> @@ -884,7 +1018,8 @@ 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> @@ -913,7 +1048,8 @@ 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> @@ -923,7 +1059,8 @@ 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> @@ -962,7 +1099,8 @@ 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> @@ -978,7 +1116,8 @@ 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> @@ -988,9 +1127,13 @@ System.out.println(10000000000 ); // Compile time error: Integer number too lar <qandadiv> <qandaentry> <question> - <para>In this exercise we look at an <code>int</code>'s the largest and smallest possible value.</para> + <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> + <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> @@ -998,12 +1141,19 @@ System.out.println(10000000000 ); // Compile time error: Integer number too lar <m:mi>2</m:mi> </m:msub> </m:math> - </inlineequation> for example represents the decimal value 5.</para> + </inlineequation> for example represents the decimal value + 5.</para> - <para>In order to represent negative values as well <xref linkend="glo_Java"/> uses <link xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's complement</link> representation. We provide some values:</para> + <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> <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> + <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> <th>Two complement representation</th> @@ -1074,7 +1224,15 @@ System.out.println(10000000000 ); // Compile time error: Integer number too lar </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> + <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) { @@ -1087,7 +1245,11 @@ System.out.println(10000000000 ); // Compile time error: Integer number too lar </question> <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> + <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) { @@ -1098,7 +1260,18 @@ System.out.println(10000000000 ); // Compile time error: Integer number too lar System.out.println("Maximum int value:" + maximum); }</programlisting> - <para>BTW: The <xref linkend="glo_JDK"/> does provide maximum value, minimum value and related information for <code>char</code>, <code>byte</code>, <code>short</code> and <code>int</code> data types int its related <classname xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html">Character</classname>, <classname xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html">Byte</classname>, <classname xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Short.html">Short</classname> and <classname xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html">Integer</classname> classes. You may want to execute:</para> + <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); @@ -1117,9 +1290,16 @@ System.out.println(10000000000 ); // Compile time error: Integer number too lar <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> + <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> - <para>The programmer tries to adopt the <link linkend="sw1QandaMaxMinInt">related int example</link> accordingly:</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; @@ -1131,23 +1311,32 @@ System.out.println("Maximum short value:" + maximum</programlisting> <itemizedlist> <listitem> - <para>The first assignment <code language="java">minumum = 0B10000000_00000000</code> is being flagged as a compile time error:</para> + <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><computeroutput>Type mismatch: cannot convert from int + to short</computeroutput></para> </listitem> <listitem> - <para>On contrary the second assignment <code language="java">maximum = 0B01111111_11111111</code> gets smoothly accepted.</para> + <para>On contrary the second assignment <code + language="java">maximum = 0B01111111_11111111</code> gets + smoothly accepted.</para> </listitem> </itemizedlist> <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> + <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> <answer> - <para>Since variables of type <code>short</code> have a two bytes representation their corresponding range is <inlineequation> + <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> @@ -1181,13 +1370,20 @@ System.out.println("Maximum short value:" + maximum</programlisting> <m:mo>]</m:mo> </m:mrow> </m:math> - </inlineequation>. Thus intensity values ranging from 0 to 255 will be easily accommodated.</para> + </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 only defines <code>int</code> literals. It does however not define <code>short</code> (or <code>byte</code>) literals.</para> + <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> - <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>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> + <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> @@ -1195,9 +1391,13 @@ System.out.println("Maximum short value:" + maximum</programlisting> <m:mi>15</m:mi> </m:msup> </m:math> - </inlineequation> (equal to 32768) exceeding a two-byte (signed) short variable's upper limit.</para> + </inlineequation> (equal to 32768) exceeding a two-byte + (signed) 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> + <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> @@ -1211,11 +1411,13 @@ System.out.println("Maximum short value:" + maximum</programlisting> </m:math> </inlineequation> (or -32768).</para> - <para>In order to avoid this error we need an explicit type conversion (a so called cast):</para> + <para>In order to avoid this error we need an explicit type + conversion (a so called cast):</para> <programlisting language="java">short minumum = (short) 0B10000000_00000000;</programlisting> - <para>Since a <code>short</code> variable's range is <inlineequation> + <para>Since a <code>short</code> variable's range is + <inlineequation> <m:math display="inline"> <m:mrow> <m:mo>[</m:mo> @@ -1249,13 +1451,17 @@ System.out.println("Maximum short value:" + maximum</programlisting> <m:mo>]</m:mo> </m:mrow> </m:math> - </inlineequation> we may as well use a unary minus sign instead:</para> + </inlineequation> we may as well use a unary minus sign + instead:</para> <programlisting language="java">short minumum = -0B10000000_00000000;</programlisting> - <para>Both variants are syntactically correct and will thus compile. However they are likely not being intended by our programmer.</para> + <para>Both variants are syntactically correct and will thus + compile. However they are likely not being intended by our + programmer.</para> - <para>The error in the first place is probably assigning a value of <inlineequation> + <para>The error in the first place is probably assigning a value + of <inlineequation> <m:math display="inline"> <m:mrow> <m:mo>+</m:mo> @@ -1267,7 +1473,8 @@ System.out.println("Maximum short value:" + maximum</programlisting> </m:msup> </m:mrow> </m:math> - </inlineequation> to a <code>short</code> variable at all.</para> + </inlineequation> to a <code>short</code> variable at + all.</para> </answer> </qandaentry> </qandadiv> @@ -1292,31 +1499,37 @@ System.out.println("Maximum short value:" + maximum</programlisting> <tr> <td><code>int</code></td> - <td><code>29</code>, <code>0b1_1101</code>, <code>0x1D</code>, <code>035</code>, <code>-29</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> + <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> + <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> + <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> + <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> @@ -1339,7 +1552,13 @@ System.out.println("Maximum short value:" + maximum</programlisting> <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> + <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 @@ -1349,15 +1568,18 @@ $: 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> + <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> + <para>Since <code>char</code> values may be assigned to + <code>int</code> variables we may code:</para> <programlisting language="java">{ char c = ' '; // space @@ -1376,7 +1598,8 @@ $: 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) ' ')); System.out.println('!' + ": " + ((int) '!')); @@ -1397,11 +1620,28 @@ System.out.println('&' + ": " + ((int) '&')); <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> - - <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> + <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> <html xmlns="http://www.w3.org/1999/xhtml"> @@ -1412,7 +1652,8 @@ System.out.println('&' + ": " + ((int) '&')); <body><h1 style="<emphasis role="bold">color: #FF0000;</emphasis>">My heading</h1></body> </html></programlisting> - <para>Write a program printing the hexadecimal representation <inlineequation> + <para>Write a program printing the hexadecimal representation + <inlineequation> <m:math display="inline"> <m:msub> <m:mi>C0</m:mi> @@ -1422,7 +1663,8 @@ System.out.println('&' + ": " + ((int) '&')); </m:math> </inlineequation> as a decimal value.</para> - <para>Complete the following code by assigning the hexadecimal value <inlineequation> + <para>Complete the following code by assigning the hexadecimal + value <inlineequation> <m:math display="inline"> <m:msub> <m:mi>C0</m:mi> @@ -1430,7 +1672,10 @@ System.out.println('&' + ": " + ((int) '&')); <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> + </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 = ...; @@ -1439,7 +1684,10 @@ System.out.println('&' + ": " + ((int) '&')); }</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> + <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> @@ -1464,7 +1712,9 @@ System.out.println(intensity);</programlisting> <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> + <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> @@ -1472,7 +1722,11 @@ System.out.println(intensity);</programlisting> </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> + <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> @@ -1481,7 +1735,8 @@ System.out.println(intensity);</programlisting> <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 + <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> @@ -1496,7 +1751,10 @@ System.out.println("Olympic winter games: " + year);</programlisting> <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> + <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> @@ -1504,14 +1762,18 @@ System.out.println("Olympic winter games: " + year);</programlisting> <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> + </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> + <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> + <para>An <code>int</code> is being represented by four + bytes in <xref linkend="glo_Java"/>.</para> </tip> </listitem> </orderedlist> @@ -1537,7 +1799,16 @@ 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> + <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); @@ -1550,12 +1821,14 @@ Decimal:932</screen> </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1QandaBracesInPrintln"> - <title>Why using braces inside <code>System.out.println(...)</code> ?</title> + <title>Why using braces inside <code>System.out.println(...)</code> + ?</title> <qandadiv> <qandaentry> <question> - <para>Solution <xref linkend="sd1QandaBinaryIntLiteral"/> contains:</para> + <para>Solution <xref linkend="sd1QandaBinaryIntLiteral"/> + contains:</para> <programlisting language="java">... System.out.println("Decimal:" + (512 + @@ -1564,16 +1837,20 @@ System.out.println("Decimal:" + (512 + 32 + 4)); ...</programlisting> - <para>Why are the <quote>inner</quote> braces immediately preceding 512 and following 4 are being required?</para> + <para>Why are the <quote>inner</quote> braces immediately + preceding 512 and following 4 are being required?</para> <tip> <itemizedlist> <listitem> - <para>Execute the above code omitting the <quote>inner</quote> braces.</para> + <para>Execute the above code omitting the + <quote>inner</quote> braces.</para> </listitem> <listitem> - <para>Read about the <quote>+</quote> operator's role e.g. in <code>System.out.println("Some" + " string").</code></para> + <para>Read about the <quote>+</quote> operator's role e.g. + in <code>System.out.println("Some" + " + string").</code></para> </listitem> </itemizedlist> </tip> @@ -1594,7 +1871,8 @@ System.out.println("Decimal:" + 512 + <screen>... Decimal:512256128324</screen> - <para>The numbers are being treated as strings rather than integer values The above code is equivalent to:</para> + <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" + @@ -1603,24 +1881,46 @@ System.out.println("Decimal:" + "512" + "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>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> + <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"/> +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 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 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> @@ -1634,12 +1934,18 @@ System.out.println("Decimal:" +<co linkends="sd1ListingPlusOpDuplicate-1" xml:id <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> + <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> + <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> + <para>Execution yields the expected output. On contrary the + following code snippet using the smiley instead does not even + compile:</para> <mediaobject> <imageobject> @@ -1650,14 +1956,20 @@ System.out.println("Character: " + arabicChar);</programlisting> <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> + <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> + <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> @@ -1677,9 +1989,14 @@ System.out.println("Character: " + arabicChar);</programlisting> <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> + </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> @@ -1723,7 +2040,9 @@ _____________________________________ </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> + <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> @@ -1742,7 +2061,8 @@ 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> + <title><uri + xlink:href="https://www.h-schmidt.net/FloatConverter">https://www.h-schmidt.net/FloatConverter</uri></title> <mediaobject> <imageobject> @@ -1787,9 +2107,13 @@ Expected value: 9223372036854775807</screen> 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).</para> + <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> - <para><emphasis role="bold">Do not execute this code yet</emphasis> but replace the TODO comments by guessing the respective variable's value.</para> + <para><emphasis role="bold">Do not execute this code + yet</emphasis> but replace the <quote>// TODO</quote> comments + by guessing the respective variable's value beforehand:</para> <para><computeroutput>a=...</computeroutput></para> @@ -1799,27 +2123,42 @@ Expected value: 9223372036854775807</screen> <para><computeroutput>e=...</computeroutput></para> - <para>After finishing your guesses execute your code and start wailing about your success rate.</para> + <para>After finishing your guesses execute your code and + (possibly) 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> + <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> + <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> + <para><code>a = x++</code> yields a ==x: The value of x + <emphasis role="bold">before</emphasis> incrementing + it.</para> </listitem> <listitem> - <para><code>a = ++x</code> yields a ==(x + 1)</para> + <para><code>a = ++x</code> yields a ==(x + 1): The value of + x <emphasis role="bold">after</emphasis> incrementing + it.</para> </listitem> </itemizedlist> - <para>The rest is just obeying the <quote>due diligence</quote> rule set:</para> + <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 @@ -1855,7 +2194,8 @@ Expected value: 9223372036854775807</screen> <m:math display="inline"> <m:mi>a</m:mi> </m:math> - </inlineequation> of a given circle having radius <inlineequation> + </inlineequation> of a given circle having radius + <inlineequation> <m:math display="inline"> <m:mi>r</m:mi> </m:math> @@ -1879,7 +2219,9 @@ Expected value: 9223372036854775807</screen> </m:mrow> </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> + </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) { @@ -1891,7 +2233,9 @@ Expected value: 9223372036854775807</screen> }</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> + <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> @@ -1916,7 +2260,10 @@ Expected value: 9223372036854775807</screen> <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> + <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) { @@ -1941,7 +2288,9 @@ Expected value: 9223372036854775807</screen> <qandadiv> <qandaentry> <question> - <para>Write an application converting temperature values being represented as degree centigrade to kelvin and Fahrenheit:</para> + <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) { @@ -1983,7 +2332,8 @@ Expected value: 9223372036854775807</screen> <orderedlist> <listitem> - <para>Write an application converting a (seconds, minutes ,hours) time specification to seconds:</para> + <para>Write an application converting a (seconds, minutes + ,hours) time specification to seconds:</para> <programlisting language="java"> public static void main(String[] args) { @@ -2003,7 +2353,9 @@ Expected value: 9223372036854775807</screen> </listitem> <listitem> - <para>Reverse the previous part of this exercise: Convert a time specification in seconds to (seconds, minutes ,hours) as in:</para> + <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) { @@ -2023,7 +2375,9 @@ 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> + <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> @@ -2080,9 +2434,14 @@ Seconds:31</screen> <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; int i = d; // Error: Incompatible types</programlisting> @@ -2091,9 +2450,16 @@ int i = d; // Error: Incompatible types</programlisting> </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> + <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> @@ -2107,9 +2473,12 @@ int i = d; // Error: Incompatible types</programlisting> <m:mi>1</m:mi> </m:mrow> </m:math> - </inlineequation>exceeding the former <code>double</code> value.</para> + </inlineequation>exceeding the former <code>double</code> + value.</para> - <para>Conclusion: <xref linkend="glo_Java"/> disallows double to int assignments unless using a so called cast (explicit type conversion):</para> + <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> @@ -2124,7 +2493,10 @@ int i = (int) d; // Explicit cast double to int</programlisting> <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> + <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> @@ -2150,7 +2522,8 @@ int i = (int) d; // Explicit cast double to int</programlisting> <m:mo>]</m:mo> </m:mrow> </m:math> - </inlineequation> to the <code>int</code>'s range of <inlineequation> + </inlineequation> to the <code>int</code>'s range of + <inlineequation> <m:math display="inline"> <m:mrow> <m:mo>[</m:mo> @@ -2318,7 +2691,10 @@ int i = (int) d; // Explicit cast double to int</programlisting> <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> @@ -2336,7 +2712,8 @@ int i = (int) d; // Explicit cast double to int</programlisting> <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> @@ -2356,7 +2733,8 @@ int i = (int) d; // Explicit cast double to int</programlisting> <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> @@ -2374,11 +2752,13 @@ int i = (int) d; // Explicit cast double to int</programlisting> <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> - <para>Joe's idea is dividing <code>long</code> values by <inlineequation> + <para>Joe's idea is dividing <code>long</code> values by + <inlineequation> <m:math display="inline"> <m:msup> <m:mi>2</m:mi> @@ -2398,7 +2778,8 @@ int i = (int) d; // Explicit cast double to int</programlisting> </m:msup> </m:mrow> </m:math> - </inlineequation> will be reduced to the intended value of <inlineequation> + </inlineequation> will be reduced to the intended value of + <inlineequation> <m:math display="inline"> <m:mrow> <m:mo>-</m:mo> @@ -2418,13 +2799,17 @@ int i = (int) d; // Explicit cast double to int</programlisting> <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> + </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 ... Exception in thread "main" java.lang.ArithmeticException: / by zero @@ -2432,15 +2817,21 @@ Exception in thread "main" java.lang.ArithmeticException: / by zero 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> + <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> + <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%"/> @@ -2526,7 +2917,8 @@ Process finished with exit code 1</screen> </tr> </informaltable> - <para>Joe's assumption for these particular values implies dividing <quote>TinyLong</quote> values by <inlineequation> + <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> @@ -2534,20 +2926,31 @@ Process finished with exit code 1</screen> <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> + </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> + <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 + 10000000_00000000_00000000_00000000 @@ -2556,14 +2959,23 @@ _____________________________________ </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> + <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> + <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> + <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> @@ -2576,13 +2988,22 @@ _____________________________________ </glossentry> <glossentry> - <glossterm><code>(int) (longValue / 2 / (Integer.MAX_VALUE + 1L))</code></glossterm> + <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> + <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> @@ -2612,17 +3033,26 @@ _____________________________________ </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> + <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; 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"> @@ -2666,7 +3096,9 @@ System.out.println(<link xlink:href="https://docs.oracle.com/javase/8/docs/api/j <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> + <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) { @@ -2688,12 +3120,14 @@ 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> + <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> + <para>We obtain the compounded interest by multiplying the + initial capital by <inlineequation> <m:math display="inline"> <m:mrow> <m:mn>1</m:mn> @@ -2709,9 +3143,11 @@ Capital after three years:233.31175902999993</screen> </m:mrow> </m:mrow> </m:math> - </inlineequation> for each year where r represents the given interest rate.</para> + </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) { @@ -2731,7 +3167,8 @@ 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) { @@ -2748,7 +3185,9 @@ 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> + <para>In <xref linkend="sd1InterestCalculator"/> we will present + a more elaborate solution based on loops and class + methods.</para> </answer> </qandaentry> </qandadiv> @@ -2775,7 +3214,8 @@ New value=-128</screen> <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; System.out.println("value=" + a); @@ -2785,14 +3225,20 @@ System.out.println("New value=" + a);</programlisting> <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> + <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> + <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> @@ -2814,18 +3260,25 @@ System.out.println("New value=" + a);</programlisting> </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; 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; @@ -2833,7 +3286,10 @@ System.out.println("New value=" + a);</programlisting> 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> + <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> @@ -2862,7 +3318,8 @@ 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> @@ -2893,10 +3350,17 @@ 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> @@ -2908,7 +3372,8 @@ 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> @@ -2918,7 +3383,8 @@ 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> @@ -2935,19 +3401,23 @@ 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> @@ -2960,12 +3430,14 @@ 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="sw1QandaCircleArea"/> 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) { @@ -2977,7 +3449,9 @@ 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) { @@ -2997,15 +3471,20 @@ 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) { @@ -3025,13 +3504,20 @@ 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> @@ -3057,11 +3543,14 @@ 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) { @@ -3072,17 +3561,24 @@ 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> @@ -3102,13 +3598,19 @@ 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/Extensions/Tdata/Components/Ref/Fig/keycard.jpg b/ws/Docbook/Extensions/Tdata/Components/Ref/Fig/keycard.jpg new file mode 100644 index 0000000000000000000000000000000000000000..3ca8ee13b40702f549c9903e101c3644877cbf23 Binary files /dev/null and b/ws/Docbook/Extensions/Tdata/Components/Ref/Fig/keycard.jpg differ diff --git a/ws/Docbook/Extensions/Tdata/Components/intro.xml b/ws/Docbook/Extensions/Tdata/Components/intro.xml index 19f0ef7bae3768bf8bc2ce697b35d5cb479c29ad..2411c443e418212870bbea29aca0f28a318edc56 100644 --- a/ws/Docbook/Extensions/Tdata/Components/intro.xml +++ b/ws/Docbook/Extensions/Tdata/Components/intro.xml @@ -1,5 +1,13 @@ <?xml version="1.0" encoding="UTF-8"?> -<chapter annotations="slide" version="5.1" xml:id="intro" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns2="http://docbook.org/ns/docbook" xmlns:ns="http://docbook.org/ns/transclusion" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:html="http://www.w3.org/1999/xhtml"> +<chapter annotations="slide" version="5.1" xml:id="intro" + xmlns="http://docbook.org/ns/docbook" + xmlns:xlink="http://www.w3.org/1999/xlink" + xmlns:xi="http://www.w3.org/2001/XInclude" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns:ns2="http://docbook.org/ns/docbook" + xmlns:ns="http://docbook.org/ns/transclusion" + xmlns:m="http://www.w3.org/1998/Math/MathML" + xmlns:html="http://www.w3.org/1999/xhtml"> <title>Introduction</title> <para>Some MathML:</para> @@ -140,7 +148,8 @@ </imageobject> </mediaobject> - <para>What about <acronym xlink:href="http://www.w3.org/TR/SVG">SVG</acronym>?</para> + <para>What about <acronym + xlink:href="http://www.w3.org/TR/SVG">SVG</acronym>?</para> <mediaobject> <imageobject> @@ -168,5 +177,11 @@ </mediaobject> </figure> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/keycard.jpg" scale="25"/> + </imageobject> + </mediaobject> + <para>This is a cross reference to <xref linkend="slideCallouts"/>.</para> </chapter>