Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
4c13972c
Commit
4c13972c
authored
10 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Two octal representation examples
parent
e5f568ae
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Doc/Sd1/extraExercise.xml
+6
-0
6 additions, 0 deletions
Doc/Sd1/extraExercise.xml
Doc/Sd1/languageFundamentals.xml
+82
-0
82 additions, 0 deletions
Doc/Sd1/languageFundamentals.xml
with
88 additions
and
0 deletions
Doc/Sd1/extraExercise.xml
+
6
−
0
View file @
4c13972c
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
Doc/Sd1/languageFundamentals.xml
+
82
−
0
View file @
4c13972c
...
...
@@ -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>
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment