Skip to content
Snippets Groups Projects
Commit b83b7307 authored by Goik Martin's avatar Goik Martin
Browse files

Correcting int to short assignment explanation

parent 422408d7
No related branches found
No related tags found
No related merge requests found
......@@ -476,7 +476,7 @@ Decimal:512256128324</programlisting>
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>
preceding <code>"Decimal:"</code> string.</para>
</callout>
</calloutlist>
</answer>
......@@ -669,11 +669,11 @@ System.out.println("Maximum short value:" + maximum);
literals. It does however not define <code>short</code> (or
<code>byte</code>) literals.</para>
<para>Thus the sequence 0B01111111_11111111 will be treated as
an integer value which is compatible with a short and is thus
assignable.</para>
<para>Thus the 0B01111111_11111111 literal will be treated as an
<code>int</code> value being compatible with a
<code>short</code> variable and is thus assignable.</para>
<para>On the other hand the binary value
<para>On the contrary the binary value
<code>10000000_00000000</code> evaluates to <inlineequation>
<m:math display="inline">
<m:msup>
......@@ -682,16 +682,13 @@ System.out.println("Maximum short value:" + maximum);
<m:mi>15</m:mi>
</m:msup>
</m:math>
</inlineequation> (equal to 32768) and thus cannot be
<emphasis>generally</emphasis> assigned to a two-byte short
variable without loosing data.</para>
</inlineequation> (equal to 32768) exceeding a two-byte short
variable's upper limit.</para>
<para>Even worse: The expression
<code>0B10000000_00000000</code> is actually a four byte
<code>int</code> literal using <link
<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. Thus its value is
<inlineequation>
complement</link> representation has got a value <inlineequation>
<m:math display="inline">
<m:mrow>
<m:mo>-</m:mo>
......@@ -703,7 +700,7 @@ System.out.println("Maximum short value:" + maximum);
</m:msup>
</m:mrow>
</m:math>
</inlineequation> (equal to -32768).</para>
</inlineequation> (or -32768).</para>
<para>In order to avoid this error we need an explicit type
conversion (a so called cast):</para>
......@@ -750,9 +747,12 @@ System.out.println("Maximum short value:" + maximum);
<programlisting language="java">short minumum = -0B10000000_00000000;</programlisting>
<para>Both variants works syntactically but this is likely not
our programmer's intended value: The error in the first place is
probably the intention to assign a value of <inlineequation>
<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>
......@@ -764,8 +764,8 @@ System.out.println("Maximum short value:" + maximum);
</m:msup>
</m:mrow>
</m:math>
</inlineequation>to a variable of type
<code>short</code>.</para>
</inlineequation> to a <code>short</code> variable at
all.</para>
</answer>
</qandaentry>
</qandadiv>
......@@ -1264,7 +1264,7 @@ Seconds:31</programlisting>
<m:mo>]</m:mo>
</m:mrow>
</m:math>
</inlineequation>. </para>
</inlineequation>.</para>
<para>Joe thinks of dividing long values by <inlineequation>
<m:math display="inline">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment