diff --git a/Doc/Sd1/LanguageFundamentals/languageFundamentals.xml b/Doc/Sd1/LanguageFundamentals/languageFundamentals.xml index 7713a3f6f9e22bb1e78719ed3f19dc12bd7349cd..19cae107671ef7479e81879153fbe25f2916e1d8 100644 --- a/Doc/Sd1/LanguageFundamentals/languageFundamentals.xml +++ b/Doc/Sd1/LanguageFundamentals/languageFundamentals.xml @@ -1164,9 +1164,14 @@ const float native super while</programlisting> <para>Modifier <code language="java">final</code> prohibits changes:</para> - <programlisting language="java">final double pi = 3.1415926; + <programlisting language="java">final double PI = 3.1415926; ... -pi = 1.0; // Compile time error: Constant cannot be modified</programlisting> +PI = 1.0; // Compile time error: Constant cannot be modified</programlisting> + + <note> + <para><code language="java">final</code> variables by convention are + being capitalized</para> + </note> </figure> <figure xml:id="sd1_fig_varCaseSensitive"> @@ -2627,14 +2632,10 @@ double pi = 3.141592653589793; pi = -4; // Woops, accidential and erroneous redefinition</programlisting> <programlisting language="java">//Good -final double pi = 3.141592653589793; +final double PI = 3.141592653589793; ... -pi = -4; // Compile time error: +PI = -4; // Compile time error: // Cannot assign a value to final variable 'pi'</programlisting> - - <programlisting language="java">// Even better -final double PI // Coding style (Best practices): Using - = 3.141592653589793; // capital letters for constant variable.</programlisting> </figure> <figure xml:id="sd1_fig_variableCategories"> @@ -3507,7 +3508,7 @@ System.out.println("Maximum short value:" + MAXIMUM);</programlisting> <answer> <para>Since variables of type <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code> - are signed having a two-byte two-complement representation their + use signed two-byte two-complement representation their corresponding range is <inlineequation> <m:math display="inline"> <m:mrow> @@ -3620,21 +3621,20 @@ System.out.println("Maximum short value:" + MAXIMUM);</programlisting> literals. The given example code is thus equivalent to:</para> <programlisting language="java">final short MINIMUM = 0B00000000_00000000_10000000_00000000, - MAXIMUM = 0B00000000_00000000_01111111_11111111; -...</programlisting> + MAXIMUM = 0B00000000_00000000_01111111_11111111;</programlisting> <para>The second <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code> - literal 0B01111111_11111111 representing 32767 is perfectly + literal 0B01111111_11111111 representing 32767 is perfectly well assignable to a <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code> - variable representing its maximum possible value as being + variable representing its maximum positive value as being intended.</para> <para>On the contrary the four-byte two complement <code language="java">int</code> value <code - language="java">00000000_00000000_10000000_00000000</code> - evaluates to <inlineequation> + language="java">0B00000000_00000000_10000000_00000000</code> + equals <inlineequation> <m:math display="inline"> <m:msup> <m:mn>2</m:mn> @@ -3642,73 +3642,53 @@ System.out.println("Maximum short value:" + MAXIMUM);</programlisting> <m:mi>15</m:mi> </m:msup> </m:math> - </inlineequation> (equal to 32768). But omitting the two - leftmost bytes consisting of zero values the remaining two-byte - two complement short value <code - language="java">10000000_00000000</code> represents -32768 (or + </inlineequation> or 32768. This is one step above <inlineequation> <m:math display="inline"> <m:mrow> - <m:mo>-</m:mo> + <m:mo>[</m:mo> - <m:msup> - <m:mi>2</m:mi> + <m:mrow> + <m:mrow> + <m:mo>-</m:mo> - <m:mi>15</m:mi> - </m:msup> - </m:mrow> - </m:math> - </inlineequation>).</para> + <m:msup> + <m:mi>2</m:mi> - <para>Avoiding this error requires representing <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>-</m:mo> + <m:mi>15</m:mi> + </m:msup> + </m:mrow> - <m:msup> - <m:mn>2</m:mn> + <m:mo>,</m:mo> - <m:mi>15</m:mi> - </m:msup> - </m:mrow> - </m:math> - </inlineequation> as an <code language="java">int</code>. Since - <inlineequation> - <m:math display="inline"> - <m:msup> - <m:mn>2</m:mn> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> - <m:mi>15</m:mi> - </m:msup> - </m:math> - </inlineequation> is being represented by - <code>00000000_00000000_10000000_00000000</code> we get its - four-byte two-complement negative counterpart by first inverting - all positions and subsequently adding the value - <quote>1</quote>:</para> + <m:mi>15</m:mi> + </m:msup> - <programlisting language="java">final short MINIMUM = 0B11111111_11111111_10000000_00000000;</programlisting> + <m:mo>-</m:mo> - <para>Alternatively we may as well use <inlineequation> - <m:math display="inline"> - <m:msup> - <m:mn>2</m:mn> + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> - <m:mi>15</m:mi> - </m:msup> + <m:mo>]</m:mo> + </m:mrow> </m:math> - </inlineequation> adding a unary minus sign:</para> - - <programlisting language="java">final short MINIMUM = -0B10000000_00000000;</programlisting> - - <para>Despite the unary minus the latter resembles our initial - faulty value.</para> - - <para>The error in the first place is probably assigning a value - of <inlineequation> + </inlineequation> and tus cannot be assigned to a <code + language="java">short</code> variable.</para> + + <para>The cast cuts off the two leading bytes of + <code>00000000_00000000_10000000_00000000</code> forcing the + remaining two lower bytes into a <code + language="java">short</code> variable + <code>10000000_00000000</code>. In 2-byte two complement + representation this is the lowest possible value <inlineequation> <m:math display="inline"> <m:mrow> - <m:mo>+</m:mo> + <m:mo>-</m:mo> <m:msup> <m:mi>2</m:mi> @@ -3717,21 +3697,7 @@ System.out.println("Maximum short value:" + MAXIMUM);</programlisting> </m:msup> </m:mrow> </m:math> - </inlineequation> to a <code language="java" - xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code> - variable mixing two-byte and four-byte two-complement - representation.</para> - - <para>Finally using a cast strips off the two leading bytes from - the int representation leaving us with the desired pattern for a - short representing -32768:</para> - - <programlisting language="none"> int short - -00000000 -00000000 -10000000 cast --> 10000000 -00000000 00000000</programlisting> + </inlineequation> or -32768.</para> </answer> </qandaentry> </qandadiv> @@ -4190,12 +4156,14 @@ _____________________________________ <figure xml:id="sd1_fig_floatPrecision"> <title>Limited precision</title> - <programlisting language="java">float float2Power31 = Integer.MAX_VALUE + 1f; // 2^31 + <programlisting language="java">float float2Power31 = <link + xlink:href="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Integer.html#MAX_VALUE">Integer.MAX_VALUE</link> + 1f; // 2^31 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> +System.out.println("Expected value: " + <link + xlink:href="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Long.html#MAX_VALUE">Long.MAX_VALUE</link>);</programlisting> <para>Result:</para> @@ -4212,11 +4180,11 @@ Expected value: 9223372036854775807 <td valign="top"><programlisting language="none">//Print 15 floating point digits DecimalFormat df = new DecimalFormat("#.###############"); -System.out.println(df.format(Float.intBitsToFloat( - 0b0_10000000_0000110011001100110011<emphasis role="red">0</emphasis> ))); +System.out.println(df.format(Float.intBitsToFloat(0b0_10000000_0000110011001100110011<emphasis + role="red">0</emphasis> ))); -System.out.println(df.format(Float.intBitsToFloat( - 0b0_10000000_0000110011001100110011<emphasis role="red">1</emphasis> )));</programlisting></td> +System.out.println(df.format(Float.intBitsToFloat(0b0_10000000_0000110011001100110011<emphasis + role="red">1</emphasis> )));</programlisting></td> </tr> <tr>