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

Two octal representation examples

parent e5f568ae
No related branches found
No related tags found
No related merge requests found
......@@ -27,6 +27,12 @@
xlink:href="https://projecteuler.net/problem=1">1</link>, <link
xlink:href="https://projecteuler.net/problem=2">2</link>, <link
xlink:href="https://projecteuler.net/problem=4">4</link>, <link
xlink:href="https://projecteuler.net/problem=5">5</link>, <link
xlink:href="https://projecteuler.net/problem=8">8</link>, <link
xlink:href="https://projecteuler.net/problem=9">9</link>, <link
xlink:href="https://projecteuler.net/problem=1">1</link>, <link
xlink:href="https://projecteuler.net/problem=1">1</link>, <link
xlink:href="https://projecteuler.net/problem=1">1</link>, <link
xlink:href="https://projecteuler.net/problem=1">1</link>, <link
xlink:href="https://projecteuler.net/problem=1">1</link>, <link
xlink:href="https://projecteuler.net/problem=1">1</link>, <link
......
......@@ -1141,4 +1141,86 @@ System.out.println("New value=" + a);</programlisting>
</qandadiv>
</qandaset>
</section>
<section xml:id="sd1fundamentalsWildThings">
<title>Wild things</title>
<qandaset defaultlabel="qanda" xml:id="sw1QandaWild">
<qandadiv>
<qandaentry>
<question>
<para>Consider the following program:</para>
<programlisting language="java"> public static void main(String[] args) {
int a = 20,
b = 3,
c = 9;
System.out.println(a + " + " + b + " + " + c + " = " + (a + b + c));
}</programlisting>
<para>This will run smoothly producing the expected output:</para>
<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>
<programlisting language="java"> public static void main(String[] args) {
int a = 20,
b = 03,
<emphasis role="bold">c = 09; // Compiler error: The literal 09 of type int is out of range</emphasis>
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>Explain the underlying cause of this error message. Why is
<code>b = 03</code> just fine in contrast to <code>c = 09</code>
?</para>
</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>
<qandaset defaultlabel="qanda" xml:id="sd1OctalOutput">
<title>Strange output</title>
<qandadiv>
<qandaentry>
<question>
<para>Consider the following code:</para>
<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>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
</chapter>
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