diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml index dd45e1dc2d7c2f9f0eda1d6c8ab4c22835ce6c81..8d9a4a24d73b37ac796829a1949393491ba515c5 100644 --- a/Doc/Sd1/languageFundamentals.xml +++ b/Doc/Sd1/languageFundamentals.xml @@ -301,7 +301,7 @@ <section xml:id="sw1IntegerLiterals"> <title>Integer value literals</title> - <section xml:id="sd1BinaryIntLiteral"> + <section xml:id="sd1SectBinaryIntLiteral"> <title>Binary literals</title> <qandaset defaultlabel="qanda" xml:id="sd1QandaBinaryIntLiteral"> @@ -336,20 +336,20 @@ <answer> <orderedlist> <listitem> - <programlisting language="java"> public static void main(String[] args) { - - System.out.println(0B1110100100); - System.out.println( 512 + - 256 + - 128 + - 32 + - 4); + <programlisting language="java">public static void main(String[] args) { + + System.out.println(" Binary:" + 0B11_10100100); + System.out.println("Decimal:" + (512 + + 256 + + 128 + + 32 + + 4)); }</programlisting> <para>This yields:</para> - <programlisting language="none">932 -932</programlisting> + <programlisting language="none"> Binary:932 +Decimal:932</programlisting> </listitem> <listitem> @@ -375,6 +375,116 @@ </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> + + <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> + + <answer> + <para>We start omitting the <quote>inner</quote> braces:</para> + + <programlisting language="java">... + System.out.println("Decimal:" + 512 + + 256 + + 128 + + 32 + + 4); ...</programlisting> + + <para>This results in the following output:</para> + + <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> + + <programlisting language="java">... + System.out.println("Decimal:" + "512" + + "256" + + "128" + + "32" + + "4"); ...</programlisting> + + <para>The <quote>+</quote> operator between two strings defines + their concatenation. So all six components get joined into a + single result string.</para> + + <para>Supplying additional inner braces defines an expression + (512 + 256 + 128 + 32 + 4) solely involving integer values. In + this context each <quote>+</quote> operator effects the usual + integer arithmetic:</para> + + <programlisting language="java">... + System.out.println("Decimal:" +<co linkends="sd1ListingPlusOpDuplicate-1" + xml:id="sd1ListingPlusOpDuplicate-1-co"/> (512 +<co + linkends="sd1ListingPlusOpDuplicate-2" + xml:id="sd1ListingPlusOpDuplicate-2-co"/> + 256 +<co + linkends="sd1ListingPlusOpDuplicate-2" + xml:id="sd1ListingPlusOpDuplicate-3-co"/> + 128 +<co + linkends="sd1ListingPlusOpDuplicate-2" + xml:id="sd1ListingPlusOpDuplicate-4-co"/> + 32 +<co + linkends="sd1ListingPlusOpDuplicate-2" + xml:id="sd1ListingPlusOpDuplicate-5-co"/> + 4)); ...</programlisting> + + <calloutlist> + <callout arearefs="sd1ListingPlusOpDuplicate-1-co" + xml:id="sd1ListingPlusOpDuplicate-1"> + <para><quote>+</quote> operator concatenating the two + strings <code>"Decimal:"</code> and <code>"932"</code>. + </para> + </callout> + + <callout arearefs="sd1ListingPlusOpDuplicate-2-co sd1ListingPlusOpDuplicate-3-co sd1ListingPlusOpDuplicate-4-co sd1ListingPlusOpDuplicate-5-co" + xml:id="sd1ListingPlusOpDuplicate-2"> + <para><quote>+</quote> operators computing the integer sum + of 512, 256, 128, 32 and 4 yielding a value of 932. This + value subsequently gets transformed into the String + <code>"932"</code> in order to be compatible with the + preceeding <code>"Decimal:"</code> string.</para> + </callout> + </calloutlist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + <section xml:id="sw1SectHexadecimalLiterals"> <title>Hexadecimal literals</title> @@ -1076,6 +1186,85 @@ Seconds:31</programlisting> </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; + final double y = b - 0.1; + System.out.println(x == y); +}</programlisting> + + <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> + + <para>Adding <code>System.out.println(x - y)</code> yields a + value of -1.1102230246251565E-16 denoting their representational + deviation.</para> + + <para>So we may compare <code>float</code> and + <code>double</code> values by providing a representational error + limit:</para> + + <programlisting language="java">final double a = 0.7; +final double b = 0.9; +final double x = a + 0.1; +final double y = b - 0.1; +System.out.println(<link + xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#abs-double-">Math.abs</link>(x - y) < 1.E-14);</programlisting> + + <para>This implements the boolean expression <inlineequation> + <m:math display="inline"> + <m:mrow> + <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:mrow> + + <m:mo><</m:mo> + + <m:msup> + <m:mi>10</m:mi> + + <m:mrow> + <m:mo>-</m:mo> + + <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>