From a1b72869cce823f06265fdd1ad1eca11244293bf Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Sun, 9 Jul 2017 12:20:48 +0200 Subject: [PATCH] New intro/exercises sections --- Doc/Sd1/languageFundamentals.xml | 4767 +++++++++++++++--------------- 1 file changed, 2397 insertions(+), 2370 deletions(-) diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml index 37219722b..931a3c99e 100644 --- a/Doc/Sd1/languageFundamentals.xml +++ b/Doc/Sd1/languageFundamentals.xml @@ -11,1154 +11,1162 @@ xmlns:db="http://docbook.org/ns/docbook"> <title>Language Fundamentals</title> - <section xml:id="sd1_fig_asciiAndUnicode"> - <title>Integer, <xref linkend="glo_ASCII"/> and <xref - linkend="glo_unicode"/></title> + <section xml:id="sd1_sect_langFundamental_intro"> + <title>Introduction</title> - <para>We will 9IUD4Gus3PJlFG241X74mainly deal with computation machines. - Having just mechanical devices at hand this still works:</para> + <section xml:id="sd1_fig_asciiAndUnicode"> + <title>Integer, <xref linkend="glo_ASCII"/> and <xref + linkend="glo_unicode"/></title> - <figure xml:id="sd1_fig_cashier"> - <title>Cash register</title> + <para>We will 9IUD4Gus3PJlFG241X74mainly deal with computation machines. + Having just mechanical devices at hand this still works:</para> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/cashRegister.png"/> - </imageobject> - </mediaobject> - </figure> + <figure xml:id="sd1_fig_cashier"> + <title>Cash register</title> - <figure xml:id="sd1_fig_zuseZ3"> - <title>Zuse Z3</title> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/cashRegister.png"/> + </imageobject> + </mediaobject> + </figure> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/zuseZ3.jpg"/> - </imageobject> - </mediaobject> - </figure> + <figure xml:id="sd1_fig_zuseZ3"> + <title>Zuse Z3</title> - <figure xml:id="sd1_fig_eniac"> - <title>Eniac</title> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/zuseZ3.jpg"/> + </imageobject> + </mediaobject> + </figure> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/eniac.jpg"/> - </imageobject> - </mediaobject> - </figure> + <figure xml:id="sd1_fig_eniac"> + <title>Eniac</title> - <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> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/eniac.jpg"/> + </imageobject> + </mediaobject> + </figure> - <figure xml:id="sd1_fig_microprocessors"> - <title>Microprocessor ICs</title> + <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> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/processors.svg"/> - </imageobject> - </mediaobject> - </figure> + <figure xml:id="sd1_fig_microprocessors"> + <title>Microprocessor ICs</title> - <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> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/processors.svg"/> + </imageobject> + </mediaobject> + </figure> - <figure xml:id="sd1_fig_z80"> - <title>Zilog Z80 CPU</title> + <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> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/z80DataBus.jpg"/> - </imageobject> - </mediaobject> - </figure> + <figure xml:id="sd1_fig_z80"> + <title>Zilog Z80 CPU</title> - <para>Following technological advances processors have been categorized by - the length the so called address- and data-bus:</para> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/z80DataBus.jpg"/> + </imageobject> + </mediaobject> + </figure> - <figure xml:id="sd1_fig_adressDataBus"> - <title>Address and data bus sizes</title> + <para>Following technological advances processors have been categorized + by the length the so called address- and data-bus:</para> - <informaltable border="1"> - <tr> - <th>Processor</th> + <figure xml:id="sd1_fig_adressDataBus"> + <title>Address and data bus sizes</title> - <th>Year</th> + <informaltable border="1"> + <tr> + <th>Processor</th> - <th>Address size / Bit</th> + <th>Year</th> - <th>Data size / Bit</th> - </tr> + <th>Address size / Bit</th> - <tr> - <td>Intel 4004</td> + <th>Data size / Bit</th> + </tr> - <td>1971</td> + <tr> + <td>Intel 4004</td> - <td>12</td> + <td>1971</td> - <td>4</td> - </tr> + <td>12</td> - <tr> - <td>Zilog Z80</td> + <td>4</td> + </tr> - <td>1976</td> + <tr> + <td>Zilog Z80</td> - <td>16</td> + <td>1976</td> - <td>8</td> - </tr> + <td>16</td> - <tr> - <td>Motorola 68000</td> + <td>8</td> + </tr> - <td>1980</td> + <tr> + <td>Motorola 68000</td> - <td>24 / 32</td> + <td>1980</td> - <td>16</td> - </tr> + <td>24 / 32</td> - <tr> - <td>Motorola 68020</td> + <td>16</td> + </tr> - <td>1984</td> + <tr> + <td>Motorola 68020</td> - <td>32</td> + <td>1984</td> - <td>32</td> - </tr> + <td>32</td> - <tr> - <td>AMD Opteron</td> + <td>32</td> + </tr> - <td>2003</td> + <tr> + <td>AMD Opteron</td> - <td>64</td> + <td>2003</td> - <td>64</td> - </tr> - </informaltable> - </figure> + <td>64</td> - <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> + <td>64</td> + </tr> + </informaltable> + </figure> - <figure xml:id="sd1_fig_signed3bitIntDual"> - <title>Signed 3 bit integer representation</title> + <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> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/dualSigned3bit.svg"/> - </imageobject> - </mediaobject> - </figure> + <figure xml:id="sd1_fig_signed3bitIntDual"> + <title>Signed 3 bit integer representation</title> - <para>Signed <code>byte</code> values are being represented - accordingly:</para> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/dualSigned3bit.svg"/> + </imageobject> + </mediaobject> + </figure> - <figure xml:id="sd1_fig_signedIntRepresentBinary"> - <title>Signed 8 bit integer binary representation</title> + <para>Signed <code>byte</code> values are being represented + accordingly:</para> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/dualSignedByte.svg"/> - </imageobject> - </mediaobject> - </figure> + <figure xml:id="sd1_fig_signedIntRepresentBinary"> + <title>Signed 8 bit integer binary representation</title> - <para>Regarding language characters we start with one of the oldest and - widespread character encoding schemes:</para> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/dualSignedByte.svg"/> + </imageobject> + </mediaobject> + </figure> - <figure xml:id="sd1_fig_7bitASCII"> - <title>7-bit <xref linkend="glo_ASCII"/></title> + <para>Regarding language characters we start with one of the oldest and + widespread character encoding schemes:</para> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/ascii7bit.svg"/> - </imageobject> - </mediaobject> - </figure> + <figure xml:id="sd1_fig_7bitASCII"> + <title>7-bit <xref linkend="glo_ASCII"/></title> - <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> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/ascii7bit.svg"/> + </imageobject> + </mediaobject> + </figure> - <figure xml:id="sd1_fig_7bitASCII_andParity"> - <title>7-bit <xref linkend="glo_ASCII"/> with even parity bit</title> + <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> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/ascii7bitEven.svg"/> - </imageobject> - </mediaobject> - </figure> + <figure xml:id="sd1_fig_7bitASCII_andParity"> + <title>7-bit <xref linkend="glo_ASCII"/> with even parity bit</title> - <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> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/ascii7bitEven.svg"/> + </imageobject> + </mediaobject> + </figure> - <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> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/iso8859_1.svg"/> - </imageobject> - </mediaobject> - </figure> - - <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> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/unicode.svg"/> - </imageobject> - </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> - </section> + <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> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/iso8859_1.svg"/> + </imageobject> + </mediaobject> + </figure> + + <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> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/unicode.svg"/> + </imageobject> + </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> + </section> - <section xml:id="sd1_sect_variables"> - <title>Variables</title> + <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> + <figure xml:id="sd1_fig_varMemoryHandle"> + <title>Handle to memory</title> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/variableToMemory.svg"/> - </imageobject> - </mediaobject> - </figure> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/variableToMemory.svg"/> + </imageobject> + </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> + <figure xml:id="sd1_fig_varDeclare"> + <title>Variable declaration</title> - <programlisting language="java">double pi;</programlisting> - </figure> + <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> + <figure xml:id="sd1_fig_varDeclareAndUse"> + <title>Declare and use</title> - <programlisting language="java">double pi; // Declaration + <programlisting language="java">double pi; // Declaration ... pi = 3.1415926; // Value assignment // Area of circle having radius 2.0 System.out.println(pi * 2.0. * 2.0);</programlisting> - </figure> + </figure> - <figure xml:id="sd1_fig_varDeclareInit"> - <title>Declaration and initialization</title> + <figure xml:id="sd1_fig_varDeclareInit"> + <title>Declaration and initialization</title> - <glosslist> - <glossentry> - <glossterm>Separate declaration and initialization</glossterm> + <glosslist> + <glossentry> + <glossterm>Separate declaration and initialization</glossterm> - <glossdef> - <programlisting language="java">double pi; // Declaration of variable pi + <glossdef> + <programlisting language="java">double pi; // Declaration of variable pi ... pi = 3.1415926; // Value assignment</programlisting> - </glossdef> - </glossentry> + </glossdef> + </glossentry> - <glossentry> - <glossterm>Combined declaration and initialization</glossterm> + <glossentry> + <glossterm>Combined declaration and initialization</glossterm> - <glossdef> - <programlisting language="java">double pi = 3.1415926;</programlisting> - </glossdef> - </glossentry> - </glosslist> - </figure> + <glossdef> + <programlisting language="java">double pi = 3.1415926;</programlisting> + </glossdef> + </glossentry> + </glosslist> + </figure> - <figure xml:id="sd1_fig_varMultiple"> - <title>Multiple variables of same type</title> + <figure xml:id="sd1_fig_varMultiple"> + <title>Multiple variables of same type</title> - <programlisting language="java">int a; + <programlisting language="java">int a; int b = 22; int c;</programlisting> - <para>is equivalent to either of:</para> + <para>is equivalent to either of:</para> - <informaltable border="1"> - <tr> - <th>Compact</th> + <informaltable border="1"> + <tr> + <th>Compact</th> - <th>Multiple lines</th> - </tr> + <th>Multiple lines</th> + </tr> - <tr> - <td valign="top"><programlisting language="java">int a, b = 22, c; </programlisting></td> + <tr> + <td valign="top"><programlisting language="java">int a, b = 22, c; </programlisting></td> - <td valign="top"><programlisting language="java">int a, + <td valign="top"><programlisting language="java">int a, b = 22, c;</programlisting></td> - </tr> - </informaltable> - </figure> - - <figure xml:id="sd1_fig_varNameConventions"> - <title>Variable naming conventions</title> - - <itemizedlist> - <listitem> - <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> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/camelCase.svg"/> - </imageobject> - </mediaobject> - </listitem> - - <listitem> - <para>Do not start with <code>_</code> or <code>$</code>.</para> - </listitem> - </itemizedlist> - </figure> - - <figure xml:id="sd1_fig_varFinal"> - <title>Constants</title> - - <programlisting language="java">final double pi = 3.1415926; + </tr> + </informaltable> + </figure> + + <figure xml:id="sd1_fig_varNameConventions"> + <title>Variable naming conventions</title> + + <itemizedlist> + <listitem> + <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> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/camelCase.svg"/> + </imageobject> + </mediaobject> + </listitem> + + <listitem> + <para>Do not start with <code>_</code> or <code>$</code>.</para> + </listitem> + </itemizedlist> + </figure> + + <figure xml:id="sd1_fig_varFinal"> + <title>Constants</title> + + <programlisting language="java">final double pi = 3.1415926; ... pi = 1.0; // Compile time error: Constant cannot be modified</programlisting> - </figure> - - <figure xml:id="sd1_fig_legalVariableName"> - <title>Variable names</title> + </figure> - <para>A variable</para> + <figure xml:id="sd1_fig_legalVariableName"> + <title>Variable names</title> - <informaltable border="1"> - <tr> - <td><itemizedlist> - <listitem> - <para>Starts with a letter</para> - </listitem> - - <listitem> - <para>May be followed by letters or digits</para> - </listitem> - - <listitem> - <para>Must not match:</para> - - <itemizedlist> - <listitem> - <para>a <xref linkend="glo_Java"/> keyword</para> - </listitem> + <para>A variable</para> - <listitem> - <para>a boolean literal</para> - </listitem> + <informaltable border="1"> + <tr> + <td><itemizedlist> + <listitem> + <para>Starts with a letter</para> + </listitem> - <listitem> - <para>The <code>null</code> literal</para> - </listitem> - </itemizedlist> - </listitem> - </itemizedlist></td> + <listitem> + <para>May be followed by letters or digits</para> + </listitem> - <td><glosslist> - <glossentry> - <glossterm>Legal</glossterm> + <listitem> + <para>Must not match:</para> - <glossdef> <itemizedlist> <listitem> - <para>$test</para> + <para>a <xref linkend="glo_Java"/> keyword</para> </listitem> <listitem> - <para>count</para> + <para>a boolean literal</para> </listitem> <listitem> - <para>blue</para> + <para>The <code>null</code> literal</para> </listitem> </itemizedlist> - </glossdef> - </glossentry> + </listitem> + </itemizedlist></td> - <glossentry> - <glossterm>Illegal</glossterm> + <td><glosslist> + <glossentry> + <glossterm>Legal</glossterm> - <glossdef> - <itemizedlist> - <listitem> - <para>2sad</para> - </listitem> + <glossdef> + <itemizedlist> + <listitem> + <para>$test</para> + </listitem> + + <listitem> + <para>count</para> + </listitem> + + <listitem> + <para>blue</para> + </listitem> + </itemizedlist> + </glossdef> + </glossentry> - <listitem> - <para>switch</para> - </listitem> + <glossentry> + <glossterm>Illegal</glossterm> - <listitem> - <para>true</para> - </listitem> - </itemizedlist> - </glossdef> - </glossentry> - </glosslist></td> - </tr> - </informaltable> - </figure> + <glossdef> + <itemizedlist> + <listitem> + <para>2sad</para> + </listitem> + + <listitem> + <para>switch</para> + </listitem> + + <listitem> + <para>true</para> + </listitem> + </itemizedlist> + </glossdef> + </glossentry> + </glosslist></td> + </tr> + </informaltable> + </figure> - <figure xml:id="sd1_fig_varCaseSensitive"> - <title>Case sensitivity</title> + <figure xml:id="sd1_fig_varCaseSensitive"> + <title>Case sensitivity</title> - <para>Variable names are case sensitive:</para> + <para>Variable names are case sensitive:</para> - <programlisting language="java">int count = 32; + <programlisting language="java">int count = 32; int Count = 44; System.out.println(count + ":" + Count); // This yields "32:44"</programlisting> - </figure> + </figure> - <figure xml:id="sd1_fig_varDeclareBeforeUse"> - <title>Declare before use</title> + <figure xml:id="sd1_fig_varDeclareBeforeUse"> + <title>Declare before use</title> - <glosslist> - <glossentry> - <glossterm>Correct:</glossterm> + <glosslist> + <glossentry> + <glossterm>Correct:</glossterm> - <glossdef> - <programlisting language="java">double f; + <glossdef> + <programlisting language="java">double f; f = -4.55;</programlisting> - </glossdef> - </glossentry> + </glossdef> + </glossentry> - <glossentry> - <glossterm>Wrong</glossterm> + <glossentry> + <glossterm>Wrong</glossterm> - <glossdef> - <programlisting language="java">f = -4.55; + <glossdef> + <programlisting language="java">f = -4.55; double f;</programlisting> - </glossdef> - </glossentry> - </glosslist> - </figure> + </glossdef> + </glossentry> + </glosslist> + </figure> - <figure xml:id="sd1_fig_typeSafety"> - <title>Type safety</title> + <figure xml:id="sd1_fig_typeSafety"> + <title>Type safety</title> - <programlisting language="java">int i = 2; + <programlisting language="java">int i = 2; int j = i; // o.K.: Assigning int to int boolean b = true; i = b; // Error: int and boolean are incompatible types i = "Hello"; // Even worse: Assigning a String to an int</programlisting> - </figure> + </figure> + + <para><xref linkend="glo_Java"/> provides meta information on + types:</para> + + <figure xml:id="sd1_fig_intTypeMaxMin"> + <title>Maximum and minimum values</title> + + <informaltable border="1"> + <tr> + <th>Type</th> + + <th>Bytes</th> + + <th>Min value</th> + + <th>Max value</th> + </tr> + + <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/constant-values.html#java.lang.Byte.MIN_VALUE"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> + + <m:msup> + <m:mi>2</m:mi> + + <m:mi>7</m:mi> + </m:msup> + </m:mrow> + </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> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> + + <m:mi>7</m:mi> + </m:msup> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation></link></td> + </tr> + + <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>0</td> + + <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> + <m:mi>2</m:mi> + + <m:mi>16</m:mi> + </m:msup> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation></link></td> + </tr> + + <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.MIN_VALUE"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> + + <m:msup> + <m:mi>2</m:mi> + + <m:mi>15</m:mi> + </m:msup> + </m:mrow> + </m:math> + </inlineequation></link></td> + + <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> + <m:mi>2</m:mi> + + <m:mi>15</m:mi> + </m:msup> - <para><xref linkend="glo_Java"/> provides meta information on - types:</para> + <m:mo>-</m:mo> - <figure xml:id="sd1_fig_intTypeMaxMin"> - <title>Maximum and minimum values</title> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation></link></td> + </tr> - <informaltable border="1"> - <tr> - <th>Type</th> + <tr> + <td>int</td> - <th>Bytes</th> + <td><link + xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.BYTES">4</link></td> - <th>Min value</th> + <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> - <th>Max value</th> - </tr> + <m:msup> + <m:mi>2</m:mi> - <tr> - <td>byte</td> + <m:mi>31</m:mi> + </m:msup> + </m:mrow> + </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> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html#BYTES">1</link></td> + <m:mi>31</m:mi> + </m:msup> - <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> + <m:mo>-</m:mo> - <m:msup> - <m:mi>2</m:mi> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation></link></td> + </tr> - <m:mi>7</m:mi> - </m:msup> - </m:mrow> - </m:math> - </inlineequation></link></td> + <tr> + <td>long</td> - <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> - <m:mi>2</m:mi> + <td><link + xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.BYTES">8</link></td> - <m:mi>7</m:mi> - </m:msup> + <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> - <m:mo>-</m:mo> + <m:msup> + <m:mi>2</m:mi> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation></link></td> - </tr> + <m:mi>63</m:mi> + </m:msup> + </m:mrow> + </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> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <tr> - <td>char</td> + <m:mi>63</m:mi> + </m:msup> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html#BYTES">2</link></td> + <m:mo>-</m:mo> - <td>0</td> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation></link></td> + </tr> + </informaltable> + </figure> - <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> - <m:mi>2</m:mi> + <figure xml:id="sd1_fig_variableCategories"> + <title>Two categories of variables</title> - <m:mi>16</m:mi> - </m:msup> + <glosslist> + <glossentry> + <glossterm>Primitive type</glossterm> - <m:mo>-</m:mo> + <glossdef> + <programlisting language="java">int a = -15;</programlisting> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation></link></td> - </tr> + <para>Possible types: All eight primitive <xref + linkend="glo_Java"/> types.</para> + </glossdef> + </glossentry> - <tr> - <td>short</td> + <glossentry> + <glossterm>Reference type</glossterm> - <td><link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Short.BYTES">2</link></td> + <glossdef> + <programlisting language="java">GpsPosition start = new GpsPosition(48.7758, 9.1829);</programlisting> - <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> + <para>Possible types: Arbitrary built in or user defined + classes.</para> + </glossdef> + </glossentry> + </glosslist> + </figure> - <m:msup> - <m:mi>2</m:mi> + <figure xml:id="sd1_fig_refTypeVariants"> + <title>Reference type variants</title> - <m:mi>15</m:mi> - </m:msup> - </m:mrow> - </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> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> - - <m:mi>15</m:mi> - </m:msup> - - <m:mo>-</m:mo> - - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation></link></td> - </tr> - - <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.MIN_VALUE"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>-</m:mo> - - <m:msup> - <m:mi>2</m:mi> - - <m:mi>31</m:mi> - </m:msup> - </m:mrow> - </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> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> - - <m:mi>31</m:mi> - </m:msup> - - <m:mo>-</m:mo> - - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation></link></td> - </tr> - - <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.MIN_VALUE"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>-</m:mo> - - <m:msup> - <m:mi>2</m:mi> - - <m:mi>63</m:mi> - </m:msup> - </m:mrow> - </m:math> - </inlineequation></link></td> - - <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> - <m:mi>2</m:mi> - - <m:mi>63</m:mi> - </m:msup> - - <m:mo>-</m:mo> - - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation></link></td> - </tr> - </informaltable> - </figure> - - <figure xml:id="sd1_fig_variableCategories"> - <title>Two categories of variables</title> - - <glosslist> - <glossentry> - <glossterm>Primitive type</glossterm> - - <glossdef> - <programlisting language="java">int a = -15;</programlisting> - - <para>Possible types: All eight primitive <xref - linkend="glo_Java"/> types.</para> - </glossdef> - </glossentry> - - <glossentry> - <glossterm>Reference type</glossterm> - - <glossdef> - <programlisting language="java">GpsPosition start = new GpsPosition(48.7758, 9.1829);</programlisting> - - <para>Possible types: Arbitrary built in or user defined - classes.</para> - </glossdef> - </glossentry> - </glosslist> - </figure> - - <figure xml:id="sd1_fig_refTypeVariants"> - <title>Reference type variants</title> - - <programlisting language="java">GpsPosition start = new GpsPosition(48.7758, 9.1829); + <programlisting language="java">GpsPosition start = new GpsPosition(48.7758, 9.1829); String name = "Simon"; LocalDate birtday = LocalDate.of(1990, Month.JULY, 5);</programlisting> - </figure> - </section> + </figure> + </section> - <section xml:id="sd1_sect_literals"> - <title>Literals</title> + <section xml:id="sd1_sect_literals"> + <title>Literals</title> - <figure xml:id="sd1_fig_literal2memory"> - <title><code>float</code> and <code>double</code></title> + <figure xml:id="sd1_fig_literal2memory"> + <title><code>float</code> and <code>double</code></title> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/literalToMemory.svg"/> - </imageobject> - </mediaobject> - </figure> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/literalToMemory.svg"/> + </imageobject> + </mediaobject> + </figure> - <figure xml:id="sd1_fig_equivalentIntRepresentations"> - <title>Four ways representing 35</title> + <figure xml:id="sd1_fig_equivalentIntRepresentations"> + <title>Four ways representing 35</title> - <informaltable border="1"> - <tr> - <th>Code</th> + <informaltable border="1"> + <tr> + <th>Code</th> - <th>Result</th> - </tr> + <th>Result</th> + </tr> - <tr> - <td><programlisting language="java">System.out.println("Decimal "+ <emphasis - role="bold">35</emphasis>); + <tr> + <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> - <td><programlisting language="none">Decimal 35 + <td><programlisting language="none">Decimal 35 Binary 35 Hex 35 Octal 35</programlisting></td> - </tr> - </informaltable> - </figure> + </tr> + </informaltable> + </figure> + </section> </section> - <section xml:id="sd1_sect_arithmeticLimitations"> - <title>Arithmetic limitations</title> + <section xml:id="sd1_sect_langFundamental_exercises"> + <title>Exercises</title> + + <section xml:id="sd1_sect_arithmeticLimitations"> + <title>Arithmetic limitations</title> - <para>Expect the unexpected:</para> + <para>Expect the unexpected:</para> - <figure xml:id="sd1_fig_byteLitteralAssign"> - <title>Strange things</title> + <figure xml:id="sd1_fig_byteLitteralAssign"> + <title>Strange things</title> - <programlisting language="java">byte count = 91; // o.K. + <programlisting language="java">byte count = 91; // o.K. byte points = 130; // Compile error: Incompatible types // Required: byte Found: int</programlisting> - </figure> + </figure> - <figure xml:id="sd1_fig_byteOverflow"> - <title>Funny things</title> + <figure xml:id="sd1_fig_byteOverflow"> + <title>Funny things</title> - <programlisting language="java">int count = 2147483647; + <programlisting language="java">int count = 2147483647; int points = 2147483647; int sum = count + points; System.out.println("Sum = " + sum);</programlisting> - <para>Result:</para> + <para>Result:</para> - <informaltable border="1"> - <tr> - <td><programlisting language="none">Sum = -2</programlisting></td> + <informaltable border="1"> + <tr> + <td><programlisting language="none">Sum = -2</programlisting></td> - <td><programlisting language="none"> 01111111_11111111_11111111_11111111 + <td><programlisting language="none"> 01111111_11111111_11111111_11111111 + 01111111_11111111_11111111_11111111 _____________________________________ 11111111_11111111_11111111_11111110</programlisting></td> - </tr> - </informaltable> - </figure> + </tr> + </informaltable> + </figure> - <para>Why not using float / double in favour of bounded <code>byte</code>, - <code>short</code>, <code>int</code>, <code>long</code> for - arithmetics?</para> + <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> + <figure xml:id="sd1_fig_floatPrecision"> + <title>Limited precision</title> - <programlisting language="java">float float2Power31 = Integer.MAX_VALUE + 1f; + <programlisting language="java">float float2Power31 = Integer.MAX_VALUE + 1f; float floatDoubleMAX_VALUE = 2 * float2Power31* float2Power31 - 1f; // 2^63 - 1 System.out.format( " Float value: %f\n", floatDoubleMAX_VALUE); System.out.println("Expected value: " + Long.MAX_VALUE);</programlisting> - <para>Result:</para> + <para>Result:</para> - <programlisting language="none"> Float value: 9223372036854776000.000000 + <programlisting language="none"> Float value: 9223372036854776000.000000 Expected value: 9223372036854775807 </programlisting> - </figure> - </section> - - <section xml:id="sw1LegalVariableName"> - <title>Legal variable names</title> + </figure> + </section> - <qandaset defaultlabel="qanda" xml:id="sw1QandaLegalVarNames"> + <section xml:id="sw1LegalVariableName"> <title>Legal variable names</title> - <qandadiv> - <qandaentry> - <question> - <para>Which of the following names are legal variable names? - Complete the following table and explain your decision with - respect to the <quote - xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc4_html_3">variables</quote> - section.</para> + <qandaset defaultlabel="qanda" xml:id="sw1QandaLegalVarNames"> + <title>Legal variable names</title> + + <qandadiv> + <qandaentry> + <question> + <para>Which of the following names are legal variable names? + Complete the following table and explain your decision with + respect to the <quote + xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc4_html_3">variables</quote> + section.</para> - <informaltable border="1"> - <colgroup width="19%"/> + <informaltable border="1"> + <colgroup width="19%"/> - <colgroup width="10%"/> + <colgroup width="10%"/> - <colgroup width="71%"/> + <colgroup width="71%"/> - <tr> - <th>Identifier</th> + <tr> + <th>Identifier</th> - <th>is legal? (yes / no)</th> + <th>is legal? (yes / no)</th> - <th>Explanation / remark</th> - </tr> + <th>Explanation / remark</th> + </tr> - <tr> - <td><code>for</code></td> + <tr> + <td><code>for</code></td> - <td/> + <td/> - <td/> - </tr> + <td/> + </tr> - <tr> - <td><code>sum_of_data</code></td> + <tr> + <td><code>sum_of_data</code></td> - <td/> + <td/> - <td/> - </tr> + <td/> + </tr> - <tr> - <td><code>sumOfData</code></td> + <tr> + <td><code>sumOfData</code></td> - <td/> + <td/> - <td/> - </tr> + <td/> + </tr> - <tr> - <td><code>first-name</code></td> + <tr> + <td><code>first-name</code></td> - <td/> + <td/> - <td/> - </tr> + <td/> + </tr> - <tr> - <td><code>ABC</code></td> + <tr> + <td><code>ABC</code></td> - <td/> + <td/> - <td/> - </tr> + <td/> + </tr> - <tr> - <td><code>42isThesolution</code></td> + <tr> + <td><code>42isThesolution</code></td> - <td/> + <td/> - <td/> - </tr> + <td/> + </tr> - <tr> - <td><code>println</code></td> + <tr> + <td><code>println</code></td> - <td/> + <td/> - <td/> - </tr> + <td/> + </tr> - <tr> - <td><code>B4</code></td> + <tr> + <td><code>B4</code></td> - <td/> + <td/> - <td/> - </tr> + <td/> + </tr> - <tr> - <td><code>AnnualSalary</code></td> + <tr> + <td><code>AnnualSalary</code></td> - <td/> + <td/> - <td/> - </tr> + <td/> + </tr> - <tr> - <td><code>"hello"</code></td> + <tr> + <td><code>"hello"</code></td> - <td/> + <td/> - <td/> - </tr> + <td/> + </tr> - <tr> - <td><code>_average </code></td> + <tr> + <td><code>_average </code></td> - <td/> + <td/> - <td/> - </tr> + <td/> + </tr> - <tr> - <td><code>ανδÏος</code></td> + <tr> + <td><code>ανδÏος</code></td> - <td/> + <td/> - <td/> - </tr> + <td/> + </tr> - <tr> - <td><code>$sum </code></td> + <tr> + <td><code>$sum </code></td> - <td/> + <td/> - <td/> - </tr> - </informaltable> + <td/> + </tr> + </informaltable> - <tip> - <para>You may want to prepare a simple <xref - linkend="glo_Java"/> program testing the above names.</para> - </tip> - </question> + <tip> + <para>You may want to prepare a simple <xref + linkend="glo_Java"/> program testing the above names.</para> + </tip> + </question> - <answer> - <para>We may write a small <xref linkend="glo_Java"/> - program:</para> + <answer> + <para>We may write a small <xref linkend="glo_Java"/> + program:</para> - <programlisting language="java">public static void main(String[] args) { + <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%"/> + <informaltable border="1"> + <colgroup width="19%"/> - <colgroup width="10%"/> + <colgroup width="10%"/> - <colgroup width="71%"/> + <colgroup width="71%"/> - <tr> - <th>Identifier</th> + <tr> + <th>Identifier</th> - <th>is legal? (yes / no)</th> + <th>is legal? (yes / no)</th> - <th>Explanation / remark</th> - </tr> + <th>Explanation / remark</th> + </tr> - <tr> - <td><code>for</code></td> + <tr> + <td><code>for</code></td> - <td>no</td> + <td>no</td> - <td><quote>for</quote> is a Java keyword.</td> - </tr> + <td><quote>for</quote> is a Java keyword.</td> + </tr> - <tr> - <td><code>sum_of_data</code></td> + <tr> + <td><code>sum_of_data</code></td> - <td>yes</td> + <td>yes</td> - <td>-</td> - </tr> + <td>-</td> + </tr> - <tr> - <td><code>sumOfData</code></td> + <tr> + <td><code>sumOfData</code></td> - <td>yes</td> + <td>yes</td> - <td>-</td> - </tr> + <td>-</td> + </tr> - <tr> - <td><code>first-name</code></td> + <tr> + <td><code>first-name</code></td> - <td>no</td> + <td>no</td> - <td>Operators like <quote>-</quote> or <quote>+</quote> must - not appear in variable names.</td> - </tr> + <td>Operators like <quote>-</quote> or <quote>+</quote> must + not appear in variable names.</td> + </tr> - <tr> - <td><code>ABC</code></td> + <tr> + <td><code>ABC</code></td> - <td>yes</td> + <td>yes</td> - <td>Best practices: Discouraged variable name, non-constant - variables should start with lowercase letters.</td> - </tr> + <td>Best practices: Discouraged variable name, non-constant + variables should start with lowercase letters.</td> + </tr> - <tr> - <td><code>42isThesolution</code></td> + <tr> + <td><code>42isThesolution</code></td> - <td>no</td> + <td>no</td> - <td>Identifiers must not start with a number.</td> - </tr> + <td>Identifiers must not start with a number.</td> + </tr> - <tr> - <td><code>println</code></td> + <tr> + <td><code>println</code></td> - <td>yes</td> + <td>yes</td> - <td><code>println</code> is a method of - <code>System.out.println(...)</code> but is no <xref - linkend="glo_Java"/> keyword.</td> - </tr> + <td><code>println</code> is a method of + <code>System.out.println(...)</code> but is no <xref + linkend="glo_Java"/> keyword.</td> + </tr> - <tr> - <td><code>B4</code></td> + <tr> + <td><code>B4</code></td> - <td>yes</td> + <td>yes</td> - <td>Best practices: Discouraged variable name, non-constant - variables should start with lowercase letters.</td> - </tr> + <td>Best practices: Discouraged variable name, non-constant + variables should start with lowercase letters.</td> + </tr> - <tr> - <td><code>AnnualSalary</code></td> + <tr> + <td><code>AnnualSalary</code></td> - <td>yes</td> + <td>yes</td> - <td>Best practices: Legal but discouraged variable name: - Non-constant variables should start with lowercase - letters.</td> - </tr> + <td>Best practices: Legal but discouraged variable name: + Non-constant variables should start with lowercase + letters.</td> + </tr> - <tr> - <td><code>"hello"</code></td> + <tr> + <td><code>"hello"</code></td> - <td>no</td> + <td>no</td> - <td>String delimiters must not be part of an identifier.</td> - </tr> + <td>String delimiters must not be part of an + identifier.</td> + </tr> - <tr> - <td><code>_average </code></td> + <tr> + <td><code>_average </code></td> - <td>yes</td> + <td>yes</td> - <td>Best practices: Should be reserved for system code.</td> - </tr> + <td>Best practices: Should be reserved for system code.</td> + </tr> - <tr> - <td><code>ανδÏος</code></td> + <tr> + <td><code>ανδÏος</code></td> - <td>yes</td> + <td>yes</td> - <td>Perfectly legal Greek <orgname>Unicode</orgname> - characters.</td> - </tr> + <td>Perfectly legal Greek <orgname>Unicode</orgname> + characters.</td> + </tr> - <tr> - <td><code>$sum </code></td> + <tr> + <td><code>$sum </code></td> - <td>yes</td> + <td>yes</td> - <td>Best practices: Using the <quote>$</quote> sign should be - reserved for system code.</td> - </tr> - </informaltable> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <td>Best practices: Using the <quote>$</quote> sign should + be reserved for system code.</td> + </tr> + </informaltable> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> - <section xml:id="sw1IntegerLiterals"> - <title>Integer value literals</title> + <section xml:id="sw1IntegerLiterals"> + <title>Integer value literals</title> - <section xml:id="sd1SectBinaryIntLiteral"> - <title>Binary literals</title> + <section xml:id="sd1SectBinaryIntLiteral"> + <title>Binary literals</title> - <qandaset defaultlabel="qanda" xml:id="sd1QandaBinaryIntLiteral"> - <qandadiv> - <qandaentry> - <question> - <orderedlist> - <listitem> - <para>Using the decimal system to represent integer values - we tend to ignore other important numbering systems. Write - an application which prints the decimal value of - <inlineequation> - <m:math display="inline"> - <m:msub> - <m:mi>1110100100</m:mi> + <qandaset defaultlabel="qanda" xml:id="sd1QandaBinaryIntLiteral"> + <qandadiv> + <qandaentry> + <question> + <orderedlist> + <listitem> + <para>Using the decimal system to represent integer values + we tend to ignore other important numbering systems. Write + an application which prints the decimal value of + <inlineequation> + <m:math display="inline"> + <m:msub> + <m:mi>1110100100</m:mi> - <m:mi>2</m:mi> - </m:msub> - </m:math> - </inlineequation> by writing the former as a binary int - literal. Verify the printed value by an independent - calculation.</para> - </listitem> + <m:mi>2</m:mi> + </m:msub> + </m:math> + </inlineequation> by writing the former as a binary int + literal. Verify the printed value by an independent + calculation.</para> + </listitem> - <listitem> - <para>Construct a second binary literal representing a - negative value of your choice.</para> + <listitem> + <para>Construct a second binary literal representing a + negative value of your choice.</para> - <tip> - <para>An <code>int</code> is being represented by four - bytes in <xref linkend="glo_Java"/>.</para> - </tip> - </listitem> - </orderedlist> - </question> + <tip> + <para>An <code>int</code> is being represented by four + bytes in <xref linkend="glo_Java"/>.</para> + </tip> + </listitem> + </orderedlist> + </question> - <answer> - <orderedlist> - <listitem> - <programlisting language="java">public static void main(String[] args) { + <answer> + <orderedlist> + <listitem> + <programlisting language="java">public static void main(String[] args) { System.out.println(" Binary:" + 0B11_10100100); System.out.println("Decimal:" + (512 @@ -1168,161 +1176,162 @@ Expected value: 9223372036854775807 + 4)); }</programlisting> - <para>This yields:</para> + <para>This yields:</para> - <programlisting language="none"> Binary:932 + <programlisting language="none"> Binary:932 Decimal:932</programlisting> - </listitem> + </listitem> - <listitem> - <para>A negative value in <link - xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representation starts with a - <quote>1</quote> at its highest bit. Binary literals in - <xref linkend="glo_Java"/> represent <code>int</code> - values. An <code>int</code> in <xref linkend="glo_Java"/> - uses 4 bytes and thus occupies 4 x 8=32 bits. Therefore - choosing a negative value is a simple task: Start with - <quote>1</quote> and let it follow by 32 - 1 = 31 random bit - values:</para> - - <programlisting language="java">public static void main(String[] args) { + <listitem> + <para>A negative value in <link + xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's + complement</link> representation starts with a + <quote>1</quote> at its highest bit. Binary literals in + <xref linkend="glo_Java"/> represent <code>int</code> + values. An <code>int</code> in <xref linkend="glo_Java"/> + uses 4 bytes and thus occupies 4 x 8=32 bits. Therefore + choosing a negative value is a simple task: Start with + <quote>1</quote> and let it follow by 32 - 1 = 31 random + bit values:</para> + + <programlisting language="java">public static void main(String[] args) { System.out.println(0B10000000_00111001_01101001_01110100); }</programlisting> - </listitem> - </orderedlist> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - - <section xml:id="sd1SectBracesInPrintln"> - <title>Why using braces inside <code>System.out.println(...)</code> - ?</title> - - <qandaset defaultlabel="qanda" xml:id="sd1QandaBracesInPrintln"> - <qandadiv> - <qandaentry> - <question> - <para>Solution <xref linkend="sd1SectBinaryIntLiteral"/> - contains:</para> - - <programlisting language="java">... + </listitem> + </orderedlist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sd1SectBracesInPrintln"> + <title>Why using braces inside <code>System.out.println(...)</code> + ?</title> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaBracesInPrintln"> + <qandadiv> + <qandaentry> + <question> + <para>Solution <xref linkend="sd1SectBinaryIntLiteral"/> + contains:</para> + + <programlisting language="java">... System.out.println("Decimal:" + (512 + 256 + 128 + 32 + 4)); ...</programlisting> - <para>Why are the <quote>inner</quote> braces immediately - preceding 512 and following 4 are being required?</para> + <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> - </listitem> + <tip> + <itemizedlist> + <listitem> + <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> - </listitem> - </itemizedlist> - </tip> - </question> + <listitem> + <para>Read about the <quote>+</quote> operator's role + e.g. in <code>System.out.println("Some" + " + string").</code></para> + </listitem> + </itemizedlist> + </tip> + </question> - <answer> - <para>We start omitting the <quote>inner</quote> braces:</para> + <answer> + <para>We start omitting the <quote>inner</quote> + braces:</para> - <programlisting language="java">... + <programlisting language="java">... System.out.println("Decimal:" + 512 + 256 + 128 + 32 + 4); ...</programlisting> - <para>This results in the following output:</para> + <para>This results in the following output:</para> - <programlisting language="none">... + <programlisting language="none">... Decimal:512256128324</programlisting> - <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">... + <programlisting language="java">... System.out.println("Decimal:" + "512" + "256" + "128" + "32" + "4"); ...</programlisting> - <para>The <quote>+</quote> operator between two strings defines - their concatenation. So all six components get joined into a - single result string.</para> + <para>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">... + <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"/> + 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"/> + linkends="sd1ListingPlusOpDuplicate-2" + xml:id="sd1ListingPlusOpDuplicate-3-co"/> 128 +<co - linkends="sd1ListingPlusOpDuplicate-2" - xml:id="sd1ListingPlusOpDuplicate-4-co"/> + linkends="sd1ListingPlusOpDuplicate-2" + xml:id="sd1ListingPlusOpDuplicate-4-co"/> 32 +<co - linkends="sd1ListingPlusOpDuplicate-2" - xml:id="sd1ListingPlusOpDuplicate-5-co"/> + linkends="sd1ListingPlusOpDuplicate-2" + xml:id="sd1ListingPlusOpDuplicate-5-co"/> 4)); ...</programlisting> - <calloutlist> - <callout arearefs="sd1ListingPlusOpDuplicate-1-co" - xml:id="sd1ListingPlusOpDuplicate-1"> - <para><quote>+</quote> operator concatenating the two - strings <code>"Decimal:"</code> and - <code>"932"</code>.</para> - </callout> - - <callout arearefs="sd1ListingPlusOpDuplicate-2-co sd1ListingPlusOpDuplicate-3-co sd1ListingPlusOpDuplicate-4-co sd1ListingPlusOpDuplicate-5-co" - xml:id="sd1ListingPlusOpDuplicate-2"> - <para><quote>+</quote> operators computing the integer sum - of 512, 256, 128, 32 and 4 yielding a value of 932. This - value subsequently gets transformed into the String - <code>"932"</code> in order to be compatible with the - preceding <code>"Decimal:"</code> string.</para> - </callout> - </calloutlist> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - - <section xml:id="sd1_sect_poorMansAscii"> - <title>Poor mans ASCII table</title> - - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_poorMansAscii"> - <qandadiv> - <qandaentry> - <question> - <para>We want to construct a list of <link - xlink:href="http://www.rapidtables.com/code/text/ascii-table.htm#print">printable - ASCII characters</link>. Write a <xref linkend="glo_Java"/> - application by starting just from from character literals - <code>' '</code>, <code>'!'</code>, <code>'"'</code>, - <code>'#'</code> and so on to show their corresponding decimal - values. The intended output is:</para> - - <programlisting language="none"> : 32 + <calloutlist> + <callout arearefs="sd1ListingPlusOpDuplicate-1-co" + xml:id="sd1ListingPlusOpDuplicate-1"> + <para><quote>+</quote> operator concatenating the two + strings <code>"Decimal:"</code> and + <code>"932"</code>.</para> + </callout> + + <callout arearefs="sd1ListingPlusOpDuplicate-2-co sd1ListingPlusOpDuplicate-3-co sd1ListingPlusOpDuplicate-4-co sd1ListingPlusOpDuplicate-5-co" + xml:id="sd1ListingPlusOpDuplicate-2"> + <para><quote>+</quote> operators computing the integer sum + of 512, 256, 128, 32 and 4 yielding a value of 932. This + value subsequently gets transformed into the String + <code>"932"</code> in order to be compatible with the + preceding <code>"Decimal:"</code> string.</para> + </callout> + </calloutlist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sd1_sect_poorMansAscii"> + <title>Poor mans ASCII table</title> + + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_poorMansAscii"> + <qandadiv> + <qandaentry> + <question> + <para>We want to construct a list of <link + xlink:href="http://www.rapidtables.com/code/text/ascii-table.htm#print">printable + ASCII characters</link>. Write a <xref linkend="glo_Java"/> + application by starting just from from character literals + <code>' '</code>, <code>'!'</code>, <code>'"'</code>, + <code>'#'</code> and so on to show their corresponding decimal + values. The intended output is:</para> + + <programlisting language="none"> : 32 !: 33 ": 34 #: 35 @@ -1330,20 +1339,21 @@ $: 36 %: 37 &: 38</programlisting> - <para>Notice the empty space being represented by decimal - 32.</para> + <para>Notice the empty space being represented by decimal + 32.</para> - <tip> - <para>A <code>char</code> value being represented by two bytes - may be easily assigned to an <code>int</code> variable.</para> - </tip> - </question> + <tip> + <para>A <code>char</code> value being represented by two + bytes may be easily assigned to an <code>int</code> + variable.</para> + </tip> + </question> - <answer> - <para>Since <code>char</code> values may be assigned to - <code>int</code> variables we may code:</para> + <answer> + <para>Since <code>char</code> values may be assigned to + <code>int</code> variables we may code:</para> - <programlisting language="java">{ + <programlisting language="java">{ char c = ' '; // space int value = c; System.out.println(c + ": " + value); @@ -1360,10 +1370,10 @@ $: 36 } ...</programlisting> - <para>Using an explicit type conversion from char to int (a so - called cast) yields an identical result:</para> + <para>Using an explicit type conversion from char to int (a so + called cast) yields an identical result:</para> - <programlisting language="java">System.out.println(' ' + ": " + ((int) ' ')); + <programlisting language="java">System.out.println(' ' + ": " + ((int) ' ')); System.out.println('!' + ": " + ((int) '!')); System.out.println('"' + ": " + ((int) '"')); System.out.println('#' + ": " + ((int) '#')); @@ -1371,35 +1381,36 @@ System.out.println('$' + ": " + ((int) '$')); System.out.println('%' + ": " + ((int) '%')); System.out.println('&' + ": " + ((int) '&')); ...</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - - <section xml:id="sw1SectHexadecimalLiterals"> - <title>Hexadecimal literals</title> - - <para>As you may know the <xref linkend="glo_RGB"/> color model uses - triplets of numbers to define color value components representing - intensities of its three base colors <foreignphrase><emphasis - role="bold">R</emphasis>ed</foreignphrase>, <foreignphrase><emphasis - role="bold">G</emphasis>reen</foreignphrase> and - <foreignphrase><emphasis role="bold">B</emphasis>lue</foreignphrase>. - The component values range from 0 to 255, the latter defining maximum - intensity.</para> - - <para>The color <quote>red</quote> for example is being represented by - (255, 0, 0). So the red component has maximum intensity while blue and - green are zero.</para> - - <para>It is however common to use <link - xlink:href="http://www.december.com/html/spec/color16codes.html">hexadecimal - in favour of decimal values</link>. Thus the same color - <quote>red</quote> in the subsequent HTML example's heading font is now - being represented by <emphasis role="bold">(FF,0,0)</emphasis>:</para> - - <programlisting language="html"><!DOCTYPE html> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sw1SectHexadecimalLiterals"> + <title>Hexadecimal literals</title> + + <para>As you may know the <xref linkend="glo_RGB"/> color model uses + triplets of numbers to define color value components representing + intensities of its three base colors <foreignphrase><emphasis + role="bold">R</emphasis>ed</foreignphrase>, <foreignphrase><emphasis + role="bold">G</emphasis>reen</foreignphrase> and + <foreignphrase><emphasis role="bold">B</emphasis>lue</foreignphrase>. + The component values range from 0 to 255, the latter defining maximum + intensity.</para> + + <para>The color <quote>red</quote> for example is being represented by + (255, 0, 0). So the red component has maximum intensity while blue and + green are zero.</para> + + <para>It is however common to use <link + xlink:href="http://www.december.com/html/spec/color16codes.html">hexadecimal + in favour of decimal values</link>. Thus the same color + <quote>red</quote> in the subsequent HTML example's heading font is + now being represented by <emphasis + role="bold">(FF,0,0)</emphasis>:</para> + + <programlisting language="html"><!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>A simple color example</title> @@ -1408,453 +1419,458 @@ System.out.println('&' + ": " + ((int) '&')); <body><h1 style="<emphasis role="bold">color: #FF0000;</emphasis>">My heading</h1></body> </html></programlisting> - <qandaset defaultlabel="qanda" xml:id="sw1QandaIntByBinary"> - <title>Integer values by binary representations</title> + <qandaset defaultlabel="qanda" xml:id="sw1QandaIntByBinary"> + <title>Integer values by binary representations</title> + + <qandadiv> + <qandaentry> + <question> + <para>Write a program printing the hexadecimal representation + <inlineequation> + <m:math display="inline"> + <m:msub> + <m:mi>C0</m:mi> + + <m:mi>16</m:mi> + </m:msub> + </m:math> + </inlineequation> as a decimal value.</para> + + <para>Complete the following code by assigning the hexadecimal + value <inlineequation> + <m:math display="inline"> + <m:msub> + <m:mi>C0</m:mi> + + <m:mi>16</m:mi> + </m:msub> + </m:math> + </inlineequation> (The <quote>silver</quote> color's all + three component's intensity in <uri + xlink:href="http://www.december.com/html/spec/color16codes.html">http://www.december.com/html/spec/color16codes.html</uri>) + to get the output in decimal representation.</para> + + <programlisting language="java">public static void main(String[] args) { + short intensity = ...; + + System.out.println(intensity); +}</programlisting> - <qandadiv> - <qandaentry> - <question> - <para>Write a program printing the hexadecimal representation - <inlineequation> - <m:math display="inline"> - <m:msub> - <m:mi>C0</m:mi> - - <m:mi>16</m:mi> - </m:msub> - </m:math> - </inlineequation> as a decimal value.</para> - - <para>Complete the following code by assigning the hexadecimal - value <inlineequation> - <m:math display="inline"> - <m:msub> - <m:mi>C0</m:mi> - - <m:mi>16</m:mi> - </m:msub> - </m:math> - </inlineequation> (The <quote>silver</quote> color's all three - component's intensity in <uri - xlink:href="http://www.december.com/html/spec/color16codes.html">http://www.december.com/html/spec/color16codes.html</uri>) - to get the output in decimal representation.</para> - - <programlisting language="java">public static void main(String[] args) { - short intensity = ...; - - System.out.println(intensity); -}</programlisting> + <tip> + <para>You may want to consider the <quote + xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc6_html_2">Literals</quote> + section of <xref linkend="bib_Kurniawan2015"/> learning + about hexadecimal integer value representation.</para> + </tip> + </question> - <tip> - <para>You may want to consider the <quote - xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc6_html_2">Literals</quote> - section of <xref linkend="bib_Kurniawan2015"/> learning about - hexadecimal integer value representation.</para> - </tip> - </question> + <answer> + <para>The solution is straightforward:</para> - <answer> - <para>The solution is straightforward:</para> - - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { short intensity = 0xC0; System.out.println(intensity); }</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - - <qandaset defaultlabel="qanda" xml:id="sw1QandaIntLimitShort"> - <title>Testing the limits, part 2 (Difficult)</title> - - <qandadiv> - <qandaentry> - <question> - <para>A careful programmer is worried whether a short variable - is <emphasis role="bold">always</emphasis> large enough to hold - color intensity values ranging from 0 to 255. Give an answer - being based on the <link - xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.1">specification</link> - and not just by try-and-error.</para> - - <para>The programmer tries to adopt the <link - linkend="sw1QandaMaxMinInt">related int example</link> - accordingly:</para> - - <programlisting language="java">short minumum = 0B10000000_00000000, + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaIntLimitShort"> + <title>Testing the limits, part 2 (Difficult)</title> + + <qandadiv> + <qandaentry> + <question> + <para>A careful programmer is worried whether a short variable + is <emphasis role="bold">always</emphasis> large enough to + hold color intensity values ranging from 0 to 255. Give an + answer being based on the <link + xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.1">specification</link> + and not just by try-and-error.</para> + + <para>The programmer tries to adopt the <link + linkend="sw1QandaMaxMinInt">related int example</link> + accordingly:</para> + + <programlisting language="java">short minumum = 0B10000000_00000000, maximum = 0B01111111_11111111; System.out.println("Minimum short value:" + minumum); System.out.println("Maximum short value:" + maximum); </programlisting> - <para>Our programmer is baffled since:</para> - - <itemizedlist> - <listitem> - <para>The first assignment <code language="java">minumum = - 0B10000000_00000000</code> is being flagged as a compile - time error:</para> + <para>Our programmer is baffled since:</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> - </listitem> - </itemizedlist> + <itemizedlist> + <listitem> + <para>The first assignment <code language="java">minumum = + 0B10000000_00000000</code> is being flagged as a compile + time error:</para> - <tip> - <para>Which integer literals types do exist according to the - <xref linkend="glo_Java"/> <link - xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1">standard</link>?</para> - </tip> - </question> + <para><computeroutput>Type mismatch: cannot convert from + int to short</computeroutput></para> + </listitem> - <answer> - <para>Since variables of type <code>short</code> are being - represented by to bytes their range is <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>[</m:mo> + <listitem> + <para>On contrary the second assignment <code + language="java">maximum = 0B01111111_11111111</code> gets + smoothly accepted.</para> + </listitem> + </itemizedlist> + <tip> + <para>Which integer literals types do exist according to the + <xref linkend="glo_Java"/> <link + xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1">standard</link>?</para> + </tip> + </question> + + <answer> + <para>Since variables of type <code>short</code> are being + represented by to bytes their range is <inlineequation> + <m:math display="inline"> <m:mrow> - <m:mrow> - <m:mo>-</m:mo> - - <m:msup> - <m:mi>2</m:mi> - - <m:mi>15</m:mi> - </m:msup> - </m:mrow> - - <m:mo>,</m:mo> + <m:mo>[</m:mo> <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <m:mrow> + <m:mo>-</m:mo> - <m:mi>15</m:mi> - </m:msup> - - <m:mo>-</m:mo> + <m:msup> + <m:mi>2</m:mi> - <m:mi>1</m:mi> - </m:mrow> - </m:mrow> + <m:mi>15</m:mi> + </m:msup> + </m:mrow> - <m:mo>]</m:mo> - </m:mrow> - </m:math> - </inlineequation>. Thus intensity values ranging from 0 to 255 - will be easily accommodated.</para> + <m:mo>,</m:mo> - <para>The second question is more difficult to explain: The - <xref linkend="glo_Java"/> standard does define <code>int</code> - literals. It does however not define <code>short</code> (or - <code>byte</code>) literals.</para> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <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> + <m:mi>15</m:mi> + </m:msup> - <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> + <m:mo>-</m:mo> - <m:mi>15</m:mi> - </m:msup> - </m:math> - </inlineequation> (equal to 32768) exceeding a two-byte short - variable's upper limit.</para> - - <para>Even worse: A <code>short</code> variable containing - binary <code>10000000_00000000</code> due to its two byte <link - xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representation has got a value <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>-</m:mo> + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> + <m:mo>]</m:mo> + </m:mrow> + </m:math> + </inlineequation>. Thus intensity values ranging from 0 to + 255 will be easily accommodated.</para> + + <para>The second question is more difficult to explain: The + <xref linkend="glo_Java"/> standard does define + <code>int</code> literals. It does however not define + <code>short</code> (or <code>byte</code>) literals.</para> + + <para>Thus the 0B01111111_11111111 literal will be treated as + an <code>int</code> value being compatible with a + <code>short</code> variable and is thus assignable.</para> + + <para>On the contrary the binary value + <code>10000000_00000000</code> evaluates to <inlineequation> + <m:math display="inline"> <m:msup> <m:mn>2</m:mn> <m:mi>15</m:mi> </m:msup> - </m:mrow> - </m:math> - </inlineequation> (or -32768).</para> - - <para>In order to avoid this error we need an explicit type - conversion (a so called cast):</para> - - <programlisting language="java">short minumum = (short) 0B10000000_00000000;</programlisting> - - <para>Since a <code>short</code> variable's range is - <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>[</m:mo> + </m:math> + </inlineequation> (equal to 32768) exceeding a two-byte + short variable's upper limit.</para> + <para>Even worse: A <code>short</code> variable containing + binary <code>10000000_00000000</code> due to its two byte + <link + xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's + complement</link> representation has got a value + <inlineequation> + <m:math display="inline"> <m:mrow> - <m:mrow> - <m:mo>-</m:mo> - - <m:msup> - <m:mi>2</m:mi> - - <m:mi>15</m:mi> - </m:msup> - </m:mrow> - - <m:mo>,</m:mo> - - <m:mrow> - <m:msup> - <m:mi>2</m:mi> - - <m:mi>15</m:mi> - </m:msup> + <m:mo>-</m:mo> - <m:mo>-</m:mo> + <m:msup> + <m:mn>2</m:mn> - <m:mi>1</m:mi> - </m:mrow> + <m:mi>15</m:mi> + </m:msup> </m:mrow> + </m:math> + </inlineequation> (or -32768).</para> - <m:mo>]</m:mo> - </m:mrow> - </m:math> - </inlineequation> we may as well use a unary minus sign - instead:</para> + <para>In order to avoid this error we need an explicit type + conversion (a so called cast):</para> - <programlisting language="java">short minumum = -0B10000000_00000000;</programlisting> + <programlisting language="java">short minumum = (short) 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>Since a <code>short</code> variable's range is + <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>[</m:mo> - <para>The error in the first place is probably assigning a value - of <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>+</m:mo> + <m:mrow> + <m:mrow> + <m:mo>-</m:mo> - <m:msup> - <m:mi>2</m:mi> + <m:msup> + <m:mi>2</m:mi> - <m:mi>15</m:mi> - </m:msup> - </m:mrow> - </m:math> - </inlineequation> to a <code>short</code> variable at - all.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <m:mi>15</m:mi> + </m:msup> + </m:mrow> - <section xml:id="sd1_sect_exerciseEmoticons"> - <title><xref linkend="glo_unicode"/> and emoticons</title> + <m:mo>,</m:mo> - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_exerciseEmoticons"> - <qandadiv> - <qandaentry> - <question> - <para>Referring to <xref linkend="sd1_fig_unicodeSample"/> we - may use a corresponding <link - xlink:href="https://en.wikibooks.org/wiki/Java_Programming/Literals#Character_Literals">character - literal</link>:</para> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <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> + <m:mi>15</m:mi> + </m:msup> - <para>Execution yields the expected output. On contrary the - following code snippet using the smiley instead does not even - compile:</para> + <m:mo>-</m:mo> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/LangFundament/emoticonInvalidCharacterConstant.png"/> - </imageobject> - </mediaobject> + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> - <para>Explain the underlying problem.</para> + <m:mo>]</m:mo> + </m:mrow> + </m:math> + </inlineequation> we may as well use a unary minus sign + instead:</para> - <tip> - <para>Consider the number of bytes being allocated for - <code>char</code> values in <xref linkend="glo_Java"/>.</para> - </tip> - </question> + <programlisting language="java">short minumum = -0B10000000_00000000;</programlisting> - <answer> - <para>According to the <link - xlink:href="https://en.wikipedia.org/wiki/Arabic_(Unicode_block)#Block">Arabic - block</link> table the code point of <code>'Úœ'</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> + <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> + <m:math display="inline"> <m:mrow> + <m:mo>+</m:mo> + <m:msup> <m:mi>2</m:mi> - <m:mi>16</m:mi> + <m:mi>15</m:mi> </m:msup> - - <m:mo>-</m:mo> - - <m:mi>1</m:mi> </m:mrow> + </m:math> + </inlineequation> to a <code>short</code> variable at + all.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sd1_sect_exerciseEmoticons"> + <title><xref linkend="glo_unicode"/> and emoticons</title> + + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_exerciseEmoticons"> + <qandadiv> + <qandaentry> + <question> + <para>Referring to <xref linkend="sd1_fig_unicodeSample"/> we + may use a corresponding <link + xlink:href="https://en.wikibooks.org/wiki/Java_Programming/Literals#Character_Literals">character + literal</link>:</para> + + <programlisting language="java">final char arabicChar = 'Úœ'; // A <link + xlink:href="https://en.wikibooks.org/wiki/Java_Programming/Literals#Character_Literals">character literal</link> +System.out.println("Character: " + arabicChar);</programlisting> - <m:mo>=</m:mo> + <para>Execution yields the expected output. On contrary the + following code snippet using the smiley instead does not even + compile:</para> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/LangFundament/emoticonInvalidCharacterConstant.png"/> + </imageobject> + </mediaobject> + + <para>Explain the underlying problem.</para> + + <tip> + <para>Consider the number of bytes being allocated for + <code>char</code> values in <xref + linkend="glo_Java"/>.</para> + </tip> + </question> + + <answer> + <para>According to the <link + xlink:href="https://en.wikipedia.org/wiki/Arabic_(Unicode_block)#Block">Arabic + block</link> table the code point of <code>'Úœ'</code> is + U+069C having a decimal equivalent of 1692.</para> + + <para><xref linkend="glo_Java"/> uses two bytes equivalent to + 16 bits for representing positive <code>char</code> values. + Thus the largest representable code point value is + <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <m:mi>65535</m:mi> - </m:mrow> - </m:math> - </inlineequation>. So <code>'Úœ'</code> fits well into this - range.</para> + <m:mi>16</m:mi> + </m:msup> - <para>According to the <link - xlink:href="https://en.wikipedia.org/wiki/Emoticons_(Unicode_block)">Emoticons - table</link> the smiley's code point value is U+1F60E being - equivalent to 128526 decimal. This exceeds the limit of 65535 - and thus prohibits the character literal in question.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <m:mo>-</m:mo> - <section xml:id="sd1SectIntMinMax"> - <title>An <code>int</code>'s minimum and maximum value</title> + <m:mi>1</m:mi> + </m:mrow> - <qandaset defaultlabel="qanda" xml:id="sw1QandaMaxMinInt"> - <qandadiv> - <qandaentry> - <question> - <para>In this exercise we look at an <code>int</code>'s the - largest and smallest possible value.</para> + <m:mo>=</m:mo> - <para>A <xref linkend="glo_Java"/> <code - xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">int</code> - is internally being represented by 4 <link - linkend="glo_byte">bytes</link>. <inlineequation> - <m:math display="inline"> - <m:msub> - <m:mi>00000000000000000000000000000101</m:mi> + <m:mi>65535</m:mi> + </m:mrow> + </m:math> + </inlineequation>. So <code>'Úœ'</code> fits well into this + range.</para> + + <para>According to the <link + xlink:href="https://en.wikipedia.org/wiki/Emoticons_(Unicode_block)">Emoticons + table</link> the smiley's code point value is U+1F60E being + equivalent to 128526 decimal. This exceeds the limit of 65535 + and thus prohibits the character literal in question.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sd1SectIntMinMax"> + <title>An <code>int</code>'s minimum and maximum value</title> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaMaxMinInt"> + <qandadiv> + <qandaentry> + <question> + <para>In this exercise we look at an <code>int</code>'s the + largest and smallest possible value.</para> + + <para>A <xref linkend="glo_Java"/> <code + xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">int</code> + is internally being represented by 4 <link + linkend="glo_byte">bytes</link>. <inlineequation> + <m:math display="inline"> + <m:msub> + <m:mi>00000000000000000000000000000101</m:mi> - <m:mi>2</m:mi> - </m:msub> - </m:math> - </inlineequation> for example represents the decimal value - 5.</para> - - <para>In order to represent negative values as well <xref - linkend="glo_Java"/> uses <link - xlink:href="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 + <m:mi>2</m:mi> + </m:msub> + </m:math> + </inlineequation> for example represents the decimal value + 5.</para> + + <para>In order to represent negative values as well <xref + linkend="glo_Java"/> uses <link xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representation of <code>int</code> - values.</caption> + complement</link> representation. We provide some + values:</para> - <tr> - <th>Two complement representation</th> + <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> - <th>Decimal representation</th> - </tr> + <tr> + <th>Two complement representation</th> - <tr> - <td><code>00000000000000000000000000000000</code></td> + <th>Decimal representation</th> + </tr> - <td>0</td> - </tr> + <tr> + <td><code>00000000000000000000000000000000</code></td> - <tr> - <td><code>01111111111111111111111111111111</code></td> + <td>0</td> + </tr> - <td><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <tr> + <td><code>01111111111111111111111111111111</code></td> - <m:mrow> - <m:mi>16</m:mi> + <td><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <m:mo>-</m:mo> + <m:mrow> + <m:mi>16</m:mi> - <m:mi>1</m:mi> - </m:mrow> - </m:msup> + <m:mo>-</m:mo> - <m:mo>-</m:mo> + <m:mi>1</m:mi> + </m:mrow> + </m:msup> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation> (Maximum)</td> - </tr> + <m:mo>-</m:mo> - <tr> - <td><code>10000000000000000000000000000000</code></td> - - <td><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>-</m:mo> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation> (Maximum)</td> + </tr> - <m:msup> - <m:mi>2</m:mi> + <tr> + <td><code>10000000000000000000000000000000</code></td> - <m:mrow> - <m:mi>16</m:mi> + <td><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> - <m:mo>-</m:mo> + <m:msup> + <m:mi>2</m:mi> - <m:mi>1</m:mi> - </m:mrow> - </m:msup> - </m:mrow> - </m:math> - </inlineequation> (Minimum)</td> - </tr> + <m:mrow> + <m:mi>16</m:mi> - <tr> - <td><code>11111111111111111111111111111111</code></td> + <m:mo>-</m:mo> - <td>-1</td> - </tr> - </table> - - <para>Use <link - xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1"><code>int</code> - literals</link> in <link - xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-BinaryNumeral">binary - representation</link> like e.g. <code>0B1100</code> in section - <quote - xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc6_html_2">Literals</quote> - in order to write an <code>int</code>'s minimum and maximum - possible value to standard output.</para> + <m:mi>1</m:mi> + </m:mrow> + </m:msup> + </m:mrow> + </m:math> + </inlineequation> (Minimum)</td> + </tr> + + <tr> + <td><code>11111111111111111111111111111111</code></td> + + <td>-1</td> + </tr> + </table> + + <para>Use <link + xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1"><code>int</code> + literals</link> in <link + xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-BinaryNumeral">binary + representation</link> like e.g. <code>0B1100</code> in section + <quote + xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc6_html_2">Literals</quote> + in order to write an <code>int</code>'s minimum and maximum + possible value to standard output.</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { int minumum = ... , //TODO: provide values by maximum = ...; // binary int literals @@ -1862,16 +1878,16 @@ System.out.println("Character: " + arabicChar);</programlisting> System.out.println("Minimum:" + minumum); System.out.println("Maximum:" + maximum); }</programlisting> - </question> + </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> + <answer> + <para>We insert <link + xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's + complement</link> representations of minimum and maximum int + values according to <xref + linkend="sw1Table4ByteIntegerRepresentation"/>.</para> - <programlisting language="java">public static void main(String[] args) { + <programlisting language="java">public static void main(String[] args) { int minumum = 0B10000000_00000000_00000000_00000000, maximum = 0B01111111_11111111_11111111_11111111; @@ -1880,76 +1896,76 @@ System.out.println("Character: " + arabicChar);</programlisting> System.out.println("Maximum int value:" + maximum); }</programlisting> - <para>BTW: The <xref linkend="glo_JDK"/> does provide maximum - value, minimum value and related information for - <code>char</code>, <code>byte</code>, <code>short</code> and - <code>int</code> data types int its related <classname - xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html">Character</classname>, - <classname - xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html">Byte</classname>, - <classname - xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Short.html">Short</classname> - and <classname - xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html">Integer</classname> - classes. You may want to execute:</para> - - <programlisting language="java"> System.out.println("int minimum:" + Integer.MIN_VALUE); + <para>BTW: The <xref linkend="glo_JDK"/> does provide maximum + value, minimum value and related information for + <code>char</code>, <code>byte</code>, <code>short</code> and + <code>int</code> data types int its related <classname + xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html">Character</classname>, + <classname + xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html">Byte</classname>, + <classname + xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Short.html">Short</classname> + and <classname + xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html">Integer</classname> + classes. You may want to execute:</para> + + <programlisting language="java"> System.out.println("int minimum:" + Integer.MIN_VALUE); System.out.println("int minimum:" + Integer.MAX_VALUE); System.out.println("int bytes:" + Integer.BYTES); System.out.println("int size:" + Integer.SIZE); </programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> </section> - </section> - <section xml:id="sw1SectSimpleExpressions"> - <title>Simple expressions</title> + <section xml:id="sw1SectSimpleExpressions"> + <title>Simple expressions</title> - <section xml:id="sd1SectCircleArea"> - <title>Calculating a circle area</title> + <section xml:id="sd1SectCircleArea"> + <title>Calculating a circle area</title> - <qandaset defaultlabel="qanda" xml:id="sw1QandaCircleArea"> - <qandadiv> - <qandaentry> - <question> - <para>The area <inlineequation> - <m:math display="inline"> - <m:mi>a</m:mi> - </m:math> - </inlineequation> of a given circle having radius - <inlineequation> - <m:math display="inline"> - <m:mi>r</m:mi> - </m:math> - </inlineequation> is being obtained by <inlineequation> - <m:math display="inline"> - <m:mrow> + <qandaset defaultlabel="qanda" xml:id="sw1QandaCircleArea"> + <qandadiv> + <qandaentry> + <question> + <para>The area <inlineequation> + <m:math display="inline"> <m:mi>a</m:mi> + </m:math> + </inlineequation> of a given circle having radius + <inlineequation> + <m:math display="inline"> + <m:mi>r</m:mi> + </m:math> + </inlineequation> is being obtained by <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mi>a</m:mi> - <m:mo>=</m:mo> + <m:mo>=</m:mo> - <m:mrow> - <m:mi>Ï€</m:mi> + <m:mrow> + <m:mi>Ï€</m:mi> - <m:mo>×</m:mo> + <m:mo>×</m:mo> - <m:msup> - <m:mi>r</m:mi> + <m:msup> + <m:mi>r</m:mi> - <m:mi>2</m:mi> - </m:msup> + <m:mi>2</m:mi> + </m:msup> + </m:mrow> </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> + </m:math> + </inlineequation>. Complete the following code to calculate + the result and write it to standard output using + <code>System.out.println(...)</code>:</para> - <programlisting language="java">public static void main(String[] args) { + <programlisting language="java">public static void main(String[] args) { double radius = 2.31; // A circle having a radius (given e.g. in mm). double pi = 3.1415926; // Constant relating a circle's radius, @@ -1958,20 +1974,20 @@ System.out.println("Character: " + arabicChar);</programlisting> // TODO: Write the circle's area to standard output }</programlisting> - <tip> - <para>You may want to read the <link - xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc1_html_4">overview - section</link> on statements.</para> - </tip> - </question> + <tip> + <para>You may want to read the <link + xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc1_html_4">overview + section</link> on statements.</para> + </tip> + </question> - <answer> - <glosslist> - <glossentry> - <glossterm>Using a mere expression</glossterm> + <answer> + <glosslist> + <glossentry> + <glossterm>Using a mere expression</glossterm> - <glossdef> - <programlisting language="java">public static void main(String[] args) { + <glossdef> + <programlisting language="java">public static void main(String[] args) { double radius = 2.31; // A circle having a radius (given e.g. in mm). double pi = 3.1415926; // Constant relating a circle's radius, @@ -1979,19 +1995,19 @@ System.out.println("Character: " + arabicChar);</programlisting> System.out.println(pi * radius * radius); }</programlisting> - </glossdef> - </glossentry> + </glossdef> + </glossentry> - <glossentry> - <glossterm>Using a variable</glossterm> + <glossentry> + <glossterm>Using a variable</glossterm> - <glossdef> - <para>Instead of immediately using the expression as an - argument to <code>System.out.println(...)</code> we may - assign its value to a variable prior to output - creation:</para> + <glossdef> + <para>Instead of immediately using the expression as an + argument to <code>System.out.println(...)</code> we may + assign its value to a variable prior to output + creation:</para> - <programlisting language="java">public static void main(String[] args) { + <programlisting language="java">public static void main(String[] args) { double radius = 2.31; // A circle having a radius (given e.g. in mm). double pi = 3.1415926; // Constant relating a circle's radius, @@ -2000,27 +2016,27 @@ System.out.println("Character: " + arabicChar);</programlisting> double area = pi * radius * radius; System.out.println(area); }</programlisting> - </glossdef> - </glossentry> - </glosslist> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - - <section xml:id="sd1SectTemperatureConvert"> - <title>Converting temperature values</title> - - <qandaset defaultlabel="qanda" xml:id="sd1QandaTemperatureConvert"> - <qandadiv> - <qandaentry> - <question> - <para>Write an application converting temperature values being - represented as degree centigrade to kelvin and - Fahrenheit:</para> - - <programlisting language="java"> public static void main(String[] args) { + </glossdef> + </glossentry> + </glosslist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sd1SectTemperatureConvert"> + <title>Converting temperature values</title> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaTemperatureConvert"> + <qandadiv> + <qandaentry> + <question> + <para>Write an application converting temperature values being + represented as degree centigrade to kelvin and + Fahrenheit:</para> + + <programlisting language="java"> public static void main(String[] args) { double temperatureCelsius = 23.2; @@ -2030,10 +2046,10 @@ System.out.println("Character: " + arabicChar);</programlisting> System.out.println("Kelvin: " + temperatureKelvin); System.out.println("Fahrenheit: " + temperatureFahrenheit); }</programlisting> - </question> + </question> - <answer> - <programlisting language="java"> public static void main(String[] args) { + <answer> + <programlisting language="java"> public static void main(String[] args) { double temperatureCelsius = 23.2; @@ -2045,27 +2061,27 @@ System.out.println("Character: " + arabicChar);</programlisting> System.out.println("Kelvin: " + temperatureKelvin); System.out.println("Fahrenheit: " + temperatureFahrenheit); }</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - - <section xml:id="sd1SectTimeUnits"> - <title>Time unit conversion</title> - - <qandaset defaultlabel="qanda" xml:id="sd1QandaTimeUnits"> - <qandadiv> - <qandaentry> - <question> - <para>This exercise consists of two parts:</para> - - <orderedlist> - <listitem> - <para>Write an application converting a (seconds, minutes - ,hours) time specification to seconds:</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sd1SectTimeUnits"> + <title>Time unit conversion</title> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaTimeUnits"> + <qandadiv> + <qandaentry> + <question> + <para>This exercise consists of two parts:</para> + + <orderedlist> + <listitem> + <para>Write an application converting a (seconds, minutes + ,hours) time specification to seconds:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { final int seconds = 31, @@ -2077,17 +2093,17 @@ System.out.println("Character: " + arabicChar);</programlisting> System.out.println("Time in seconds:" + timeInSeconds); }</programlisting> - <para>The expected output reads:</para> + <para>The expected output reads:</para> - <programlisting language="none">Time in seconds:15391</programlisting> - </listitem> + <programlisting language="none">Time in seconds:15391</programlisting> + </listitem> - <listitem> - <para>Reverse the previous part of this exercise: Convert a - time specification in seconds to (seconds, minutes ,hours) - as in:</para> + <listitem> + <para>Reverse the previous part of this exercise: Convert + a time specification in seconds to (seconds, minutes + ,hours) as in:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { final int timeInSeconds = 15391; @@ -2098,27 +2114,28 @@ System.out.println("Character: " + arabicChar);</programlisting> System.out.println("Seconds:" + seconds); }</programlisting> - <para>The expected output reads:</para> + <para>The expected output reads:</para> - <programlisting language="none">Hours:4 + <programlisting language="none">Hours:4 Minutes:16 Seconds:31</programlisting> - <tip> - <para>Consider the <link - xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html">remainder - operator <quote>%</quote></link> (modulo operator).</para> - </tip> - </listitem> - </orderedlist> - </question> + <tip> + <para>Consider the <link + xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html">remainder + operator <quote>%</quote></link> (modulo + operator).</para> + </tip> + </listitem> + </orderedlist> + </question> - <answer> - <orderedlist> - <listitem> - <para>A straightforward solution reads:</para> + <answer> + <orderedlist> + <listitem> + <para>A straightforward solution reads:</para> - <para><programlisting language="java"> public static void main(String[] args) { + <para><programlisting language="java"> public static void main(String[] args) { final int seconds = 31, @@ -2129,10 +2146,10 @@ Seconds:31</programlisting> System.out.println("Time in seconds:" + timeInSeconds); }</programlisting></para> - </listitem> + </listitem> - <listitem> - <programlisting language="java"> public static void main(String[] args) { + <listitem> + <programlisting language="java"> public static void main(String[] args) { final int timeInSeconds = 15391; @@ -2146,618 +2163,622 @@ Seconds:31</programlisting> System.out.println("Minutes:" + minutes); System.out.println("Seconds:" + seconds); }</programlisting> - </listitem> - </orderedlist> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - - <section xml:id="sd1_sect_typeAssignCompat"> - <title>Assignment and type safety</title> - - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_typeAssignCompat"> - <qandadiv> - <qandaentry> - <question> - <para>We consider:</para> - - <programlisting language="java">int i = -21; + </listitem> + </orderedlist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sd1_sect_typeAssignCompat"> + <title>Assignment and type safety</title> + + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_typeAssignCompat"> + <qandadiv> + <qandaentry> + <question> + <para>We consider:</para> + + <programlisting language="java">int i = -21; double d = i;</programlisting> - <para>This snippet shows perfectly correct <xref - linkend="glo_Java"/> code. However <xref - linkend="sd1_fig_typeSafety"/> suggests we should not be allowed - to assign an <code>int</code> to a <code>double</code> value due - to type safety.</para> + <para>This snippet shows perfectly correct <xref + linkend="glo_Java"/> code. However <xref + linkend="sd1_fig_typeSafety"/> suggests we should not be + allowed to assign an <code>int</code> to a <code>double</code> + value due to type safety.</para> - <para>Even worse: Swapping types yields a compile time - error:</para> + <para>Even worse: Swapping types yields a compile time + error:</para> - <programlisting language="java">double d = 2.4; + <programlisting language="java">double d = 2.4; int i = d; // Error: Incompatible types</programlisting> - <para>Why is this? Make an educated guess.</para> - </question> - - <answer> - <para>The first snippet compiles by using the widening - conversion: When assigning <code>d = i</code> the <xref - linkend="glo_Java"/> compiler implicitly converts the - <code>int</code> into a <code>double</code> value.</para> - - <para>Turning a double into an int is more cumbersome: The - expression <code>i = 3.5</code> could be evaluated by agreeing - on a specific rounding prescription. But what about i = - 3457357385783573478955345.45? A <xref linkend="glo_Java"/> - <code>int</code>'s maximum value is <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <para>Why is this? Make an educated guess.</para> + </question> + + <answer> + <para>The first snippet compiles by using the widening + conversion: When assigning <code>d = i</code> the <xref + linkend="glo_Java"/> compiler implicitly converts the + <code>int</code> into a <code>double</code> value.</para> + + <para>Turning a double into an int is more cumbersome: The + expression <code>i = 3.5</code> could be evaluated by agreeing + on a specific rounding prescription. But what about i = + 3457357385783573478955345.45? A <xref linkend="glo_Java"/> + <code>int</code>'s maximum value is <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <m:mi>31</m:mi> - </m:msup> + <m:mi>31</m:mi> + </m:msup> - <m:mo>-</m:mo> + <m:mo>-</m:mo> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation>exceeding the former <code>double</code> - value.</para> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </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; + <programlisting language="java">double d = 2.4; int i = (int) d; // Explicit cast double to int</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - - <section xml:id="sd1SectLong2Float"> - <title>Reducing <code>long</code> to <code>int</code></title> - - <qandaset defaultlabel="qanda" xml:id="sd1QandaLong2Float"> - <qandadiv> - <qandaentry> - <question> - <para>Joe programmer intends to map positive <code>long</code> - values to <code>int</code> values. This requires scaling down - <quote>half</quote> the <code>long</code> data type's range - <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>[</m:mo> - + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sd1SectLong2Float"> + <title>Reducing <code>long</code> to <code>int</code></title> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaLong2Float"> + <qandadiv> + <qandaentry> + <question> + <para>Joe programmer intends to map positive <code>long</code> + values to <code>int</code> values. This requires scaling down + <quote>half</quote> the <code>long</code> data type's range + <inlineequation> + <m:math display="inline"> <m:mrow> - <m:mn>0</m:mn> - - <m:mo>,</m:mo> + <m:mo>[</m:mo> <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <m:mn>0</m:mn> - <m:mi>63</m:mi> - </m:msup> + <m:mo>,</m:mo> - <m:mo>-</m:mo> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <m:mi>1</m:mi> - </m:mrow> - </m:mrow> + <m:mi>63</m:mi> + </m:msup> - <m:mo>]</m:mo> - </m:mrow> - </m:math> - </inlineequation> to the <code>int</code>'s range of - <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>[</m:mo> + <m:mo>-</m:mo> - <m:mrow> - <m:mn>0</m:mn> + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> - <m:mo>,</m:mo> + <m:mo>]</m:mo> + </m:mrow> + </m:math> + </inlineequation> to the <code>int</code>'s range of + <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>[</m:mo> <m:mrow> - <m:msup> - <m:mi>2</m:mi> - - <m:mi>31</m:mi> - </m:msup> - - <m:mo>-</m:mo> + <m:mn>0</m:mn> - <m:mi>1</m:mi> - </m:mrow> - </m:mrow> + <m:mo>,</m:mo> - <m:mo>]</m:mo> - </m:mrow> - </m:math> - </inlineequation>:</para> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <informaltable border="1"> - <colgroup width="25%"/> + <m:mi>31</m:mi> + </m:msup> - <colgroup width="30%"/> + <m:mo>-</m:mo> - <colgroup width="15%"/> - - <colgroup width="30%"/> + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> - <tr> - <th colspan="2">From</th> + <m:mo>]</m:mo> + </m:mrow> + </m:math> + </inlineequation>:</para> - <th colspan="2">To</th> - </tr> + <informaltable border="1"> + <colgroup width="25%"/> - <tr> - <th><code>long</code></th> + <colgroup width="30%"/> - <th><code>remark</code></th> + <colgroup width="15%"/> - <th><code>int</code></th> + <colgroup width="30%"/> - <th>remark</th> - </tr> + <tr> + <th colspan="2">From</th> - <tr> - <td align="right">0</td> + <th colspan="2">To</th> + </tr> - <td align="right"/> + <tr> + <th><code>long</code></th> - <td align="right">0</td> + <th><code>remark</code></th> - <td align="right"/> - </tr> + <th><code>int</code></th> - <tr> - <td align="right">1</td> + <th>remark</th> + </tr> - <td align="right"/> + <tr> + <td align="right">0</td> - <td align="right">0</td> + <td align="right"/> - <td align="right"/> - </tr> + <td align="right">0</td> - <tr> - <td align="center" colspan="2">...</td> + <td align="right"/> + </tr> - <td align="right">0</td> + <tr> + <td align="right">1</td> - <td align="center">...</td> - </tr> + <td align="right"/> - <tr> - <td align="right">4294967295</td> + <td align="right">0</td> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <td align="right"/> + </tr> - <m:mi>32</m:mi> - </m:msup> + <tr> + <td align="center" colspan="2">...</td> - <m:mo>-</m:mo> + <td align="right">0</td> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation></td> + <td align="center">...</td> + </tr> - <td align="right">0</td> + <tr> + <td align="right">4294967295</td> - <td align="right"/> - </tr> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <tr> - <td align="right">4294967296</td> + <m:mi>32</m:mi> + </m:msup> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:msup> - <m:mi>2</m:mi> + <m:mo>-</m:mo> - <m:mi>32</m:mi> - </m:msup> - </m:math> - </inlineequation></td> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation></td> - <td align="right">1</td> + <td align="right">0</td> - <td align="right"/> - </tr> + <td align="right"/> + </tr> - <tr> - <td align="right">4294967297</td> + <tr> + <td align="right">4294967296</td> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:mrow> + <td align="right"><inlineequation> + <m:math display="inline"> <m:msup> <m:mi>2</m:mi> <m:mi>32</m:mi> </m:msup> + </m:math> + </inlineequation></td> - <m:mo>+</m:mo> + <td align="right">1</td> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation></td> + <td align="right"/> + </tr> - <td align="right">1</td> + <tr> + <td align="right">4294967297</td> - <td align="right"/> - </tr> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <tr> - <td align="center" colspan="4">...</td> - </tr> + <m:mi>32</m:mi> + </m:msup> - <tr> - <td align="right">9223372036854775806</td> + <m:mo>+</m:mo> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation></td> - <m:mi>63</m:mi> - </m:msup> + <td align="right">1</td> - <m:mo>-</m:mo> + <td align="right"/> + </tr> - <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> + <tr> + <td align="center" colspan="4">...</td> + </tr> - <td align="right">2147483647</td> + <tr> + <td align="right">9223372036854775806</td> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <m:mi>31</m:mi> - </m:msup> + <m:mi>63</m:mi> + </m:msup> - <m:mo>-</m:mo> + <m:mo>-</m:mo> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation> or <varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> - </tr> + <m: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> - <tr> - <td align="right">9223372036854775807</td> + <td align="right">2147483647</td> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <m:mi>63</m:mi> - </m:msup> + <m:mi>31</m:mi> + </m:msup> - <m:mo>-</m:mo> + <m:mo>-</m:mo> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation> or <varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></td> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation> or <varname + xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> + </tr> - <td align="right">2147483647</td> + <tr> + <td align="right">9223372036854775807</td> - <td align="right"><inlineequation> - <m:math display="inline"> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <m:mi>31</m:mi> - </m:msup> + <m:mi>63</m:mi> + </m:msup> - <m:mo>-</m:mo> + <m:mo>-</m:mo> - <m:mi>1</m:mi> - </m:mrow> - </m:math> - </inlineequation> or <varname - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> - </tr> - </informaltable> + <m:mi>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> - <para>Joe's idea is dividing <code>long</code> values by - <inlineequation> - <m:math display="inline"> - <m:msup> - <m:mi>2</m:mi> + <td align="right">2147483647</td> - <m:mi>32</m:mi> - </m:msup> - </m:math> - </inlineequation>. As a result a long value of <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>-</m:mo> + <td align="right"><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> + + <m:mi>31</m:mi> + </m:msup> + + <m:mo>-</m:mo> + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation> or <varname + xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</varname></td> + </tr> + </informaltable> + + <para>Joe's idea is dividing <code>long</code> values by + <inlineequation> + <m:math display="inline"> <m:msup> <m:mi>2</m:mi> - <m:mi>63</m:mi> + <m:mi>32</m:mi> </m:msup> - </m:mrow> - </m:math> - </inlineequation> will be reduced to the intended value of - <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>-</m:mo> + </m:math> + </inlineequation>. As a result a long value of + <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> + + <m:msup> + <m:mi>2</m:mi> + + <m:mi>63</m:mi> + </m:msup> + </m:mrow> + </m:math> + </inlineequation> will be reduced to the intended value of + <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> + + <m:msup> + <m:mi>2</m:mi> + <m:mi>31</m:mi> + </m:msup> + </m:mrow> + </m:math> + </inlineequation>. Since <inlineequation> + <m:math display="inline"> <m:msup> <m:mi>2</m:mi> - <m:mi>31</m:mi> + <m:mi>32</m:mi> </m:msup> - </m:mrow> - </m:math> - </inlineequation>. Since <inlineequation> - <m:math display="inline"> - <m:msup> - <m:mi>2</m:mi> - - <m:mi>32</m:mi> - </m:msup> - </m:math> - </inlineequation> seems to be equal to <code>2 * (<link - xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</link> - + 1))</code> (why?) Joe's first attempt reads:</para> + </m:math> + </inlineequation> seems to be equal to <code>2 * (<link + xlink:href="https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</link> + + 1))</code> (why?) Joe's first attempt reads:</para> - <programlisting language="java" linenumbering="numbered" - startinglinenumber="26">final long longValue = 2147483648L; + <programlisting language="java" linenumbering="numbered" + startinglinenumber="26">final long longValue = 2147483648L; final int reducedValue = (int) (longValue / (2 * (Integer.MAX_VALUE + 1))); System.out.println(reducedValue);</programlisting> - <para>Unfortunately the results are not promising. This code - merely results in a runtime error:</para> + <para>Unfortunately the results are not promising. This code + merely results in a runtime error:</para> - <programlisting language="none">/usr/lib/jvm/java-8-oracle/bin/java ... + <programlisting language="none">/usr/lib/jvm/java-8-oracle/bin/java ... Exception in thread "main" java.lang.ArithmeticException: / by zero at qq.App.main(App.java:27) Process finished with exit code 1</programlisting> - <para>Explain the underlying problem and correct Joe's - error.</para> + <para>Explain the underlying problem and correct Joe's + error.</para> - <tip> - <para>It may be helpful thinking of a smaller example before. - Consider hypothetic integer types <quote>TinyLong</quote> and - <quote>TinyInt</quote> requiring 4 and 2 bits - respectively.</para> - </tip> - </question> + <tip> + <para>It may be helpful thinking of a smaller example + before. Consider hypothetic integer types + <quote>TinyLong</quote> and <quote>TinyInt</quote> requiring + 4 and 2 bits respectively.</para> + </tip> + </question> - <answer> - <para>As a prerequisite we represent the scaling of a hypothetic - integer type <quote>TinyLong</quote> to a smaller type - <quote>TinyInt</quote> of 4 and 2 bits respectively:</para> + <answer> + <para>As a prerequisite we represent the scaling of a + hypothetic integer type <quote>TinyLong</quote> to a smaller + type <quote>TinyInt</quote> of 4 and 2 bits + respectively:</para> - <informaltable border="1"> - <colgroup width="20%"/> + <informaltable border="1"> + <colgroup width="20%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <colgroup width="5%"/> + <colgroup width="5%"/> - <tr> - <td><quote>TinyLong</quote>, n = 4</td> + <tr> + <td><quote>TinyLong</quote>, n = 4</td> - <td>-8</td> + <td>-8</td> - <td>-7</td> + <td>-7</td> - <td>-6</td> + <td>-6</td> - <td>-5</td> + <td>-5</td> - <td>-4</td> + <td>-4</td> - <td>-3</td> + <td>-3</td> - <td>-2</td> + <td>-2</td> - <td>-1</td> + <td>-1</td> - <td>0</td> + <td>0</td> - <td>1</td> + <td>1</td> - <td>2</td> + <td>2</td> - <td>3</td> + <td>3</td> - <td>4</td> + <td>4</td> - <td>5</td> + <td>5</td> - <td>6</td> + <td>6</td> - <td>7</td> - </tr> + <td>7</td> + </tr> - <tr> - <td><quote>TinyInt</quote>, n = 2</td> + <tr> + <td><quote>TinyInt</quote>, n = 2</td> - <td colspan="4">-2</td> + <td colspan="4">-2</td> - <td colspan="4">-1</td> + <td colspan="4">-1</td> - <td colspan="4">0</td> + <td colspan="4">0</td> - <td colspan="4">1</td> - </tr> - </informaltable> + <td colspan="4">1</td> + </tr> + </informaltable> - <para>Joe's assumption for these particular values implies - dividing <quote>TinyLong</quote> values by <inlineequation> - <m:math display="inline"> - <m:msup> - <m:mi>2</m:mi> + <para>Joe's assumption for these particular values implies + dividing <quote>TinyLong</quote> values by <inlineequation> + <m:math display="inline"> + <m:msup> + <m:mi>2</m:mi> - <m:mi>2</m:mi> - </m:msup> - </m:math> - </inlineequation> (truncating). This indeed yields the desired - result for non-negative values. So why does Joe encounter a - division by zero runtime exception when executing - <code>longValue / (2 * (Integer.MAX_VALUE + 1))</code> ?</para> + <m:mi>2</m:mi> + </m:msup> + </m:math> + </inlineequation> (truncating). This indeed yields the + desired result for non-negative values. So why does Joe + encounter a division by zero runtime exception when executing + <code>longValue / (2 * (Integer.MAX_VALUE + 1))</code> + ?</para> - <para>Unfortunately Joe's implementation is seriously flawed for - even two reasons:</para> + <para>Unfortunately Joe's implementation is seriously flawed + for even two reasons:</para> - <orderedlist> - <listitem> - <para>The constant <code>Integer.MAX_VALUE</code> already - suggests we will not be able to increase its value while - staying as an <code>int</code>. The expression - <code>Integer.MAX_VALUE + 1</code> will be evaluated using - <code>int</code> rather than <code>long</code> arithmetic - thus returning <code>Integer.MIN_VALUE</code> (negative!) - due to an arithmetic overflow:</para> - - <programlisting language="java"> 01111111_11111111_11111111_11111111 + <orderedlist> + <listitem> + <para>The constant <code>Integer.MAX_VALUE</code> already + suggests we will not be able to increase its value while + staying as an <code>int</code>. The expression + <code>Integer.MAX_VALUE + 1</code> will be evaluated using + <code>int</code> rather than <code>long</code> arithmetic + thus returning <code>Integer.MIN_VALUE</code> (negative!) + due to an arithmetic overflow:</para> + + <programlisting language="java"> 01111111_11111111_11111111_11111111 + 00000000_00000000_00000000_00000001 _____________________________________ 10000000_00000000_00000000_00000000</programlisting> - <para>The expression <code>2 * (Integer.MAX_VALUE + - 1)</code> then gives rise to a second overflow error:</para> + <para>The expression <code>2 * (Integer.MAX_VALUE + + 1)</code> then gives rise to a second overflow + error:</para> - <programlisting language="java"> 10000000_00000000_00000000_00000000 + <programlisting language="java"> 10000000_00000000_00000000_00000000 + 10000000_00000000_00000000_00000000 _____________________________________ 00000000_00000000_00000000_00000000</programlisting> - </listitem> - </orderedlist> - - <para>Both errors combined (surprisingly) result in a value of - <code>0</code> explaining the <quote>division by zero</quote> - error message. There are two possible solutions:</para> - - <glosslist> - <glossentry> - <glossterm><code>(int) (longValue / (2L * (Integer.MAX_VALUE - + 1L)))</code></glossterm> - - <glossdef> - <para>Introducing <code>2L</code> or <code>1L</code> (one - is sufficient) in favour of simply using 2 and 1 turns - both addition and multiplication into operations involving - at least one long argument. Thus for both operations the - <xref linkend="glo_Java"/> runtime will use - <code>long</code> arithmetic returning the desired - <quote>reducing</quote> factor of <inlineequation> - <m:math display="inline"> - <m:msup> - <m:mi>2</m:mi> - - <m:mi>32</m:mi> - </m:msup> - </m:math> - </inlineequation> of type <code>long</code>.</para> - </glossdef> - </glossentry> - - <glossentry> - <glossterm><code>(int) (longValue / 2 / (Integer.MAX_VALUE + - 1L))</code></glossterm> - - <glossdef> - <para>Same result as before.</para> - - <note> - <para>This time the expression starts with - <code>longValue / 2 ...</code> Since the variable - <code>longValue</code> is of type <code>long</code> the - expression <code>longValue / 2</code> will be evaluated - by the <xref linkend="glo_Java"/> runtime using - <code>long</code> arithmetics. The result will - subsequently be multiplied with <code>Integer.MAX_VALUE - + 1L</code> again using <code>long</code> - arithmetic.</para> - </note> - </glossdef> - </glossentry> - </glosslist> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - - <section xml:id="sd1SectFloatPrecisionProblem"> - <title>Representational float and double miracles</title> - - <qandaset defaultlabel="qanda" xml:id="sd1QandaFloatPrecisionProblem"> - <qandadiv> - <qandaentry> - <question> - <para>Consider and execute the following code snippet:</para> - - <programlisting language="java">public static void main(String[] args) { + </listitem> + </orderedlist> + + <para>Both errors combined (surprisingly) result in a value of + <code>0</code> explaining the <quote>division by zero</quote> + error message. There are two possible solutions:</para> + + <glosslist> + <glossentry> + <glossterm><code>(int) (longValue / (2L * + (Integer.MAX_VALUE + 1L)))</code></glossterm> + + <glossdef> + <para>Introducing <code>2L</code> or <code>1L</code> + (one is sufficient) in favour of simply using 2 and 1 + turns both addition and multiplication into operations + involving at least one long argument. Thus for both + operations the <xref linkend="glo_Java"/> runtime will + use <code>long</code> arithmetic returning the desired + <quote>reducing</quote> factor of <inlineequation> + <m:math display="inline"> + <m:msup> + <m:mi>2</m:mi> + + <m:mi>32</m:mi> + </m:msup> + </m:math> + </inlineequation> of type <code>long</code>.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm><code>(int) (longValue / 2 / (Integer.MAX_VALUE + + 1L))</code></glossterm> + + <glossdef> + <para>Same result as before.</para> + + <note> + <para>This time the expression starts with + <code>longValue / 2 ...</code> Since the variable + <code>longValue</code> is of type <code>long</code> + the expression <code>longValue / 2</code> will be + evaluated by the <xref linkend="glo_Java"/> runtime + using <code>long</code> arithmetics. The result will + subsequently be multiplied with + <code>Integer.MAX_VALUE + 1L</code> again using + <code>long</code> arithmetic.</para> + </note> + </glossdef> + </glossentry> + </glosslist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sd1SectFloatPrecisionProblem"> + <title>Representational float and double miracles</title> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaFloatPrecisionProblem"> + <qandadiv> + <qandaentry> + <question> + <para>Consider and execute the following code snippet:</para> + + <programlisting language="java">public static void main(String[] args) { final double a = 0.7; final double b = 0.9; final double x = a + 0.1; @@ -2765,80 +2786,82 @@ _____________________________________ System.out.println(x == y); }</programlisting> - <para>Explain the result and supply a possible solution.</para> - </question> + <para>Explain the result and supply a possible + solution.</para> + </question> - <answer> - <para>The expression <code>x == y</code> evaluates to - <code>false</code>. This surprising result is due to limited - precision regarding both <code>float</code> and - <code>double</code> IEEE representations: A given value will be - approximated as close as possible.</para> + <answer> + <para>The expression <code>x == y</code> evaluates to + <code>false</code>. This surprising result is due to limited + precision regarding both <code>float</code> and + <code>double</code> IEEE representations: A given value will + be approximated as close as possible.</para> - <para>Adding <code>System.out.println(x - y)</code> yields a - value of -1.1102230246251565E-16 denoting the representational - deviation of <code>x</code> and <code>y</code>.</para> + <para>Adding <code>System.out.println(x - y)</code> yields a + value of -1.1102230246251565E-16 denoting the representational + deviation of <code>x</code> and <code>y</code>.</para> - <para>So we may compare <code>float</code> and - <code>double</code> values by providing a representational error - limit:</para> + <para>So we may compare <code>float</code> and + <code>double</code> values by providing a representational + error limit:</para> - <programlisting language="java">final double a = 0.7; + <programlisting language="java">final double a = 0.7; final double b = 0.9; final double x = a + 0.1; final double y = b - 0.1; System.out.println(<link - xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#abs-double-">Math.abs</link>(x - y) < 1.E-14);</programlisting> + 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"> - <m:mrow> + <para>This implements the boolean expression <inlineequation> + <m:math display="inline"> <m:mrow> - <m:mo>|</m:mo> - <m:mrow> - <m:mi>x</m:mi> - - <m:mo>-</m:mo> - - <m:mi>y</m:mi> - </m:mrow> + <m:mo>|</m:mo> - <m:mo>|</m:mo> - </m:mrow> - - <m:mo><</m:mo> + <m:mrow> + <m:mi>x</m:mi> - <m:msup> - <m:mi>10</m:mi> + <m:mo>-</m:mo> - <m:mrow> - <m:mo>-</m:mo> + <m:mi>y</m:mi> + </m:mrow> - <m:mi>14</m:mi> + <m:mo>|</m:mo> </m:mrow> - </m:msup> - </m:mrow> - </m:math> - </inlineequation>.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - <section xml:id="sd1SectInterestSimple"> - <title>Interest calculation</title> + <m:mo><</m:mo> - <qandaset defaultlabel="qanda" xml:id="sd1QandaInterestSimple"> - <qandadiv> - <qandaentry> - <question> - <para>We want to calculate the compounded interest starting from - an initial capital, a given annual interest rate and a duration - of three years. Consider the following code fragment:</para> + <m:msup> + <m:mi>10</m:mi> + + <m:mrow> + <m:mo>-</m:mo> - <programlisting language="java"> public static void main(String[] args) { + <m:mi>14</m:mi> + </m:mrow> + </m:msup> + </m:mrow> + </m:math> + </inlineequation>.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sd1SectInterestSimple"> + <title>Interest calculation</title> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaInterestSimple"> + <qandadiv> + <qandaentry> + <question> + <para>We want to calculate the compounded interest starting + from an initial capital, a given annual interest rate and a + duration of three years. Consider the following code + fragment:</para> + + <programlisting language="java"> public static void main(String[] args) { final double initialCapital = 223.12; final double interestRate = 1.5; @@ -2851,43 +2874,43 @@ System.out.println(<link System.out.println("Capital after three years:" + ...); }</programlisting> - <para>The expected output is:</para> + <para>The expected output is:</para> - <programlisting language="none">Initial capital:223.12 + <programlisting language="none">Initial capital:223.12 Annual interest rate:1.5 Capital after three years:233.31175902999993</programlisting> - <tip> - <para>In case you are unsure read about calculating the - compounded interest.</para> - </tip> - </question> + <tip> + <para>In case you are unsure read about calculating the + compounded interest.</para> + </tip> + </question> - <answer> - <para>We obtain the compounded interest by multiplying the - initial capital by <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mn>1</m:mn> + <answer> + <para>We obtain the compounded interest by multiplying the + initial capital by <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mn>1</m:mn> - <m:mo>+</m:mo> + <m:mo>+</m:mo> - <m:mrow> - <m:mi>r</m:mi> + <m:mrow> + <m:mi>r</m:mi> - <m:mo>/</m:mo> + <m:mo>/</m:mo> - <m:mn>100</m:mn> + <m:mn>100</m:mn> + </m:mrow> </m:mrow> - </m:mrow> - </m:math> - </inlineequation> for each year where r represents the given - interest rate.</para> + </m:math> + </inlineequation> for each year where r represents the given + interest rate.</para> - <para>Since we have not yet introduced loops this multiplication - has to be repeated three times:</para> + <para>Since we have not yet introduced loops this + multiplication has to be repeated three times:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { final double initialCapital = 223.12; final double interestRate = 1.5; @@ -2905,10 +2928,10 @@ Capital after three years:233.31175902999993</programlisting> System.out.println("Capital after three years:" + capitalAtThreeYears); }</programlisting> - <para>We might as well use a single arithmetic expression to - achieve the same result:</para> + <para>We might as well use a single arithmetic expression to + achieve the same result:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { final double initialCapital = 223.12; final double interestRate = 1.5; @@ -2923,133 +2946,133 @@ Capital after three years:233.31175902999993</programlisting> System.out.println("Capital after three years:" + capitalAtThreeYears); }</programlisting> - <para>In <xref linkend="sd1InterestCalculator"/> we will present - a more elaborate solution based on loops and class - methods.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <para>In <xref linkend="sd1InterestCalculator"/> we will + present a more elaborate solution based on loops and class + methods.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> - <section xml:id="sd1ExpressionStrangeThings"> - <title>Strange things happen</title> + <section xml:id="sd1ExpressionStrangeThings"> + <title>Strange things happen</title> - <qandaset defaultlabel="qanda" xml:id="sw1QandaByteOverflow"> - <title>Byte integer arithmetics</title> + <qandaset defaultlabel="qanda" xml:id="sw1QandaByteOverflow"> + <title>Byte integer arithmetics</title> - <qandadiv> - <qandaentry> - <question> - <para>Consider the following code snippet:</para> + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code snippet:</para> - <programlisting language="java">byte a = 127; + <programlisting language="java">byte a = 127; System.out.println("value=" + a); a++; System.out.println("New value=" + a); </programlisting> - <para>This will produce the following output:</para> + <para>This will produce the following output:</para> - <programlisting language="none">value=127 + <programlisting language="none">value=127 New value=-128</programlisting> - <para>Explain this strange behaviour.</para> + <para>Explain this strange behaviour.</para> - <para>Moreover you'll find the following code snippet yields a - compile time error:</para> + <para>Moreover you'll find the following code snippet yields a + compile time error:</para> - <programlisting language="java">byte a = 127; + <programlisting language="java">byte a = 127; System.out.println("value=" + a); a = a + 1; // Error: Type mismatch: cannot convert from int to byte System.out.println("New value=" + a);</programlisting> - <para>Explain this error's cause.</para> + <para>Explain this error's cause.</para> - <tip> - <para>You may want to read the <link - xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc1_html_4">overview - section</link> on statements.</para> - </tip> - </question> + <tip> + <para>You may want to read the <link + xlink:href="https://proquest.safaribooksonline.com/9780992133047/toc1_html_4">overview + section</link> on statements.</para> + </tip> + </question> - <answer> - <para>A byte variable ranges from -128 to +127. Thus - incrementing 127 by 1 yields -128 and thus an overflow - error.</para> + <answer> + <para>A byte variable ranges from -128 to +127. Thus + incrementing 127 by 1 yields -128 and thus an overflow + error.</para> - <para>Since <xref linkend="glo_Java"/> uses <link - xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representation we have:</para> + <para>Since <xref linkend="glo_Java"/> uses <link + xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's + complement</link> representation we have:</para> - <informaltable border="1"> - <tr> - <td><code> 01111111</code></td> + <informaltable border="1"> + <tr> + <td><code> 01111111</code></td> - <td><code> 127</code></td> - </tr> + <td><code> 127</code></td> + </tr> - <tr> - <td><code>+00000001</code></td> + <tr> + <td><code>+00000001</code></td> - <td><code> +1</code></td> - </tr> + <td><code> +1</code></td> + </tr> - <tr> - <td><code>=10000000</code></td> + <tr> + <td><code>=10000000</code></td> - <td><code>=-128</code></td> - </tr> - </informaltable> + <td><code>=-128</code></td> + </tr> + </informaltable> - <para>On machine level the above calculation is just an ordinary - addition.</para> + <para>On machine level the above calculation is just an + ordinary addition.</para> - <para>Conclusion: Watch out when doing (integer) - arithmetic!</para> + <para>Conclusion: Watch out when doing (integer) + arithmetic!</para> - <para>The compile time error is due to the definition of the - <quote>+</quote> operator in Java always returning an - <code>int</code> rather than a byte. Consider:</para> + <para>The compile time error is due to the definition of the + <quote>+</quote> operator in Java always returning an + <code>int</code> rather than a byte. Consider:</para> - <programlisting language="java"> byte a = 120, b = 10; + <programlisting language="java"> byte a = 120, b = 10; System.out.println(a + b);</programlisting> - <para>This yields the expected output of 130 and corresponds to - an <code>int</code> value.</para> + <para>This yields the expected output of 130 and corresponds + to an <code>int</code> value.</para> - <para>If the expression <code>a + b</code> was of data type - <code>byte</code> an arithmetic overflow as in the subsequent - code example would occur:</para> + <para>If the expression <code>a + b</code> was of data type + <code>byte</code> an arithmetic overflow as in the subsequent + code example would occur:</para> - <programlisting language="java"> byte a = 120, b = 10; + <programlisting language="java"> byte a = 120, b = 10; byte sum = (byte) (a + b); System.out.println(sum);</programlisting> - <para>The explicit type conversion (a so called type cast or - cast for short) forces the 4-byte integer into a one-byte - variable <code>sum</code> thereby loosing the original value and - returning -126 instead.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <para>The explicit type conversion (a so called type cast or + cast for short) forces the 4-byte integer into a one-byte + variable <code>sum</code> thereby loosing the original value + and returning -126 instead.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> - <section xml:id="sw1SectAssignArithmetics"> - <title>Assignments and simple arithmetics</title> + <section xml:id="sw1SectAssignArithmetics"> + <title>Assignments and simple arithmetics</title> - <qandaset defaultlabel="qanda" xml:id="sw1QandaGuessingResults"> - <title>Guessing results</title> + <qandaset defaultlabel="qanda" xml:id="sw1QandaGuessingResults"> + <title>Guessing results</title> - <qandadiv> - <qandaentry> - <question> - <para>Consider the following code segment:</para> + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code segment:</para> - <programlisting language="java"> int a = 3; + <programlisting language="java"> int a = 3; a++; //Incrementing a by 1 --> a==4 int b = a; // TODO @@ -3069,57 +3092,57 @@ System.out.println("New value=" + a);</programlisting> System.out.println("c=" + c); System.out.println("e=" + e);</programlisting> - <para>We want to guess the expected result. Copy/paste the above - code into a main() method body within your IDE of choice - (probably Eclipse). Though it is tempting to just execute this - code ant watch the results <emphasis role="bold">please refrain - from doing so!</emphasis></para> + <para>We want to guess the expected result. Copy/paste the + above code into a main() method body within your IDE of choice + (probably Eclipse). Though it is tempting to just execute this + code ant watch the results <emphasis role="bold">please + refrain from doing so!</emphasis></para> - <para>Instead start a <quote>dry run</quote> complete the above - comments to predict the expected outcome:</para> + <para>Instead start a <quote>dry run</quote> complete the + above comments to predict the expected outcome:</para> - <para><computeroutput>a=...</computeroutput></para> + <para><computeroutput>a=...</computeroutput></para> - <para><computeroutput>b=...</computeroutput></para> + <para><computeroutput>b=...</computeroutput></para> - <para><computeroutput>c=...</computeroutput></para> + <para><computeroutput>c=...</computeroutput></para> - <para><computeroutput>e=...</computeroutput></para> + <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 start + wailing about your success rate.</para> - <tip> - <para>Both <code>x++</code> (postfix notation) and - <code>++x</code> (infix notation) are expressions themselves - which might even be rewritten as <code>(x++)</code> and - <code>(++x)</code> for the sake of clarity. The difference is - not about operator precedence rules but simply about the - values of these expressions when being assigned to other - variables.</para> - </tip> - </question> + <tip> + <para>Both <code>x++</code> (postfix notation) and + <code>++x</code> (infix notation) are expressions themselves + which might even be rewritten as <code>(x++)</code> and + <code>(++x)</code> for the sake of clarity. The difference + is not about operator precedence rules but simply about the + values of these expressions when being assigned to other + variables.</para> + </tip> + </question> - <answer> - <para>As inferred by the hint the biggest problem is about - understanding postfix and infix notation of the operators - <code>++</code> and <code>--</code>. A corresponding expression - evaluates to:</para> + <answer> + <para>As inferred by the hint the biggest problem is about + understanding postfix and infix notation of the operators + <code>++</code> and <code>--</code>. A corresponding + expression evaluates to:</para> - <itemizedlist> - <listitem> - <para><code>a = x++</code> yields a ==x</para> - </listitem> + <itemizedlist> + <listitem> + <para><code>a = x++</code> yields a ==x</para> + </listitem> - <listitem> - <para><code>a = ++x</code> yields a ==(x + 1)</para> - </listitem> - </itemizedlist> + <listitem> + <para><code>a = ++x</code> yields a ==(x + 1)</para> + </listitem> + </itemizedlist> - <para>The rest is just obeying the <quote>due diligence</quote> - rule set:</para> + <para>The rest is just obeying the <quote>due + diligence</quote> rule set:</para> - <programlisting language="java"> int a = 3; + <programlisting language="java"> int a = 3; a++; //Incrementing a by 1 --> a==4 int b = a; // Assigning value of a --> b==4 @@ -3138,39 +3161,39 @@ System.out.println("New value=" + a);</programlisting> System.out.println("b=" + b); System.out.println("c=" + c); System.out.println("e=" + e);</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> </section> - </section> - <section xml:id="sw1StringLiterals"> - <title>Dealing with strings</title> + <section xml:id="sw1StringLiterals"> + <title>Dealing with strings</title> - <qandaset defaultlabel="qanda" xml:id="sw1QandaStringVariableMix"> - <title>Composing strings of literals and variables</title> + <qandaset defaultlabel="qanda" xml:id="sw1QandaStringVariableMix"> + <title>Composing strings of literals and variables</title> - <qandadiv> - <qandaentry> - <question> - <para>Consider the following code:</para> + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { final int games = 3, playersPerGame = 22; // ToDo ... }</programlisting> - <para>Complete the above snippet to produce the following - output:</para> + <para>Complete the above snippet to produce the following + output:</para> - <programlisting language="none">3 Games having 22 players each results in 66 players altogether.</programlisting> - </question> + <programlisting language="none">3 Games having 22 players each results in 66 players altogether.</programlisting> + </question> - <answer> - <programlisting language="java"> public static void main(String[] args) { + <answer> + <programlisting language="java"> public static void main(String[] args) { final int games = 3, playersPerGame = 22; System.out.println(games + " Games having " + playersPerGame @@ -3178,113 +3201,113 @@ System.out.println("New value=" + a);</programlisting> + " players altogether."); }</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <qandaset defaultlabel="qanda" xml:id="sw1QandaStringEscapeDoubleQuote"> - <title>Escaping double quotes</title> + <qandaset defaultlabel="qanda" xml:id="sw1QandaStringEscapeDoubleQuote"> + <title>Escaping double quotes</title> - <qandadiv> - <qandaentry> - <question> - <para>Consider the following code:</para> + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code:</para> - <programlisting language="java">public static void main(String[] args) { + <programlisting language="java">public static void main(String[] args) { 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> - - <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> - </tip> - </question> - - <answer> - <para>There are at least two solutions on offer:</para> - - <glosslist> - <glossentry> - <glossterm>Perfectly obvious</glossterm> - - <glossdef> - <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> - </glossentry> - - <glossentry> - <glossterm>Even more clumsy</glossterm> - - <glossdef> - <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> - </glossentry> - </glosslist> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - - <qandaset defaultlabel="qanda" xml:id="sw1QandaStringSuppExercise"> - <title>Supplementary string exercises</title> - - <qandadiv> - <qandaentry> - <question> - <para xml:id="sw1QandaStringCodingBatExtra">Solve the following - external exercises:</para> - - <itemizedlist> - <listitem> - <para - xlink:href="http://codingbat.com/prob/p171896">helloName</para> - </listitem> - - <listitem> - <para - xlink:href="http://codingbat.com/prob/p161056">makeAbba</para> - </listitem> - - <listitem> - <para - xlink:href="http://codingbat.com/prob/p147483">makeTags</para> - </listitem> - </itemizedlist> - </question> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <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> + </tip> + </question> + + <answer> + <para>There are at least two solutions on offer:</para> + + <glosslist> + <glossentry> + <glossterm>Perfectly obvious</glossterm> + + <glossdef> + <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> + </glossentry> + + <glossentry> + <glossterm>Even more clumsy</glossterm> + + <glossdef> + <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> + </glossentry> + </glosslist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaStringSuppExercise"> + <title>Supplementary string exercises</title> + + <qandadiv> + <qandaentry> + <question> + <para xml:id="sw1QandaStringCodingBatExtra">Solve the following + external exercises:</para> + + <itemizedlist> + <listitem> + <para + xlink:href="http://codingbat.com/prob/p171896">helloName</para> + </listitem> + + <listitem> + <para + xlink:href="http://codingbat.com/prob/p161056">makeAbba</para> + </listitem> + + <listitem> + <para + xlink:href="http://codingbat.com/prob/p147483">makeTags</para> + </listitem> + </itemizedlist> + </question> + </qandaentry> + </qandadiv> + </qandaset> + </section> - <section xml:id="sw1LanguageFundamentalUsingFinal"> - <title>Using <code>final</code></title> + <section xml:id="sw1LanguageFundamentalUsingFinal"> + <title>Using <code>final</code></title> - <qandaset defaultlabel="qanda" xml:id="sw1QandaCircleAreaFinal"> - <title>Calculating the area of a circle avoiding accidental - redefinition</title> + <qandaset defaultlabel="qanda" xml:id="sw1QandaCircleAreaFinal"> + <title>Calculating the area of a circle avoiding accidental + redefinition</title> - <qandadiv> - <qandaentry> - <question> - <para>In Exercise <xref linkend="sd1SectCircleArea"/> you - calculated a given circle's area:</para> + <qandadiv> + <qandaentry> + <question> + <para>In Exercise <xref linkend="sd1SectCircleArea"/> you + calculated a given circle's area:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { double radius = 2.31; // A circle having a radius (given e.g. in mm). double pi = 3.1415926; // Constant relating a circle's radius, perimeter @@ -3294,11 +3317,11 @@ 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) { + <programlisting language="java"> public static void main(String[] args) { double radius = 2.31; // A circle having a radius (given e.g. in mm). double pi = 3.1415926; // Constant relating a circle's radius, perimeter @@ -3316,21 +3339,22 @@ 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> - </tip> - </question> + <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> + </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> + <answer> + <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) { + <programlisting language="java" linenumbering="numbered">public static void main(String[] args) { double radius = 2.31; // A circle's radius (given e.g. in mm). <emphasis role="bold">final</emphasis> double pi = 3.1415926; // Creating pi as a constant (non-modifiable/ @@ -3348,36 +3372,36 @@ 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> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <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> + </qandaset> + </section> - <section xml:id="sd1fundamentalsWildThings"> - <title>Wild things</title> + <section xml:id="sd1fundamentalsWildThings"> + <title>Wild things</title> - <qandaset defaultlabel="qanda" xml:id="sw1QandaWild"> - <qandadiv> - <qandaentry> - <question> - <para>Consider the following program:</para> + <qandaset defaultlabel="qanda" xml:id="sw1QandaWild"> + <qandadiv> + <qandaentry> + <question> + <para>Consider the following program:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { int a = 20, b = 3, @@ -3387,15 +3411,16 @@ 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> - <programlisting language="none">20 + 3 + 9 = 32</programlisting> + <programlisting language="none">20 + 3 + 9 = 32</programlisting> - <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) { + <programlisting language="java"> public static void main(String[] args) { int a = 20, b = 03, @@ -3404,58 +3429,60 @@ 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> - </tip> - </question> + <tip> + <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> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + <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> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <qandaset defaultlabel="qanda" xml:id="sd1OctalOutput"> - <title>Strange output</title> + <qandaset defaultlabel="qanda" xml:id="sd1OctalOutput"> + <title>Strange output</title> - <qandadiv> - <qandaentry> - <question> - <para>Consider the following code:</para> + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code:</para> - <programlisting language="java"> public static void main(String[] args) { + <programlisting language="java"> public static void main(String[] args) { int a = 041; System.out.println("Value = " + a); }</programlisting> - <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> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + <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> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> </section> </chapter> -- GitLab