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
7bed2714
Commit
7bed2714
authored
9 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
byte sum conversion clarification
parent
ea1be0ef
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Doc/Sd1/languageFundamentals.xml
+31
-2
31 additions, 2 deletions
Doc/Sd1/languageFundamentals.xml
with
31 additions
and
2 deletions
Doc/Sd1/languageFundamentals.xml
+
31
−
2
View file @
7bed2714
...
@@ -876,14 +876,18 @@ System.out.println("New value=" + a);
...
@@ -876,14 +876,18 @@ System.out.println("New value=" + a);
<programlisting
language=
"none"
>
value=127
<programlisting
language=
"none"
>
value=127
New value=-128
</programlisting>
New value=-128
</programlisting>
<para>
Explain this strange behaviour. Moreover you'll find the
<para>
Explain this strange behaviour.
</para>
following code snippet yields a compile time error:
</para>
<para>
Moreover you'll find the following code snippet yields a
compile time error:
</para>
<programlisting
language=
"noneJava"
>
byte a = 127;
<programlisting
language=
"noneJava"
>
byte a = 127;
System.out.println("value=" + a);
System.out.println("value=" + a);
a = a + 1; // Error: Type mismatch: cannot convert from int to byte
a = a + 1; // Error: Type mismatch: cannot convert from int to byte
System.out.println("New value=" + a);
</programlisting>
System.out.println("New value=" + a);
</programlisting>
<para>
Explain this error's cause.
</para>
<tip>
<tip>
<para>
You may want to read the
<link
<para>
You may want to read the
<link
xlink:href=
"http://proquest.safaribooksonline.com/9780992133047/toc1_html_4"
>
overview
xlink:href=
"http://proquest.safaribooksonline.com/9780992133047/toc1_html_4"
>
overview
...
@@ -925,6 +929,31 @@ System.out.println("New value=" + a);</programlisting>
...
@@ -925,6 +929,31 @@ System.out.println("New value=" + a);</programlisting>
<para>
Conclusion: Watch out when doing (integer)
<para>
Conclusion: Watch out when doing (integer)
arithmetic!
</para>
arithmetic!
</para>
<para>
The compile time error is due to the definition of the
<quote>
+
</quote>
operator in Java always returning an
<code>
int
</code>
rather than a byte. Consider:
</para>
<programlisting
language=
"none"
>
byte a = 120, b = 10;
System.out.println(a + b);
</programlisting>
<para>
This yields the expected output of 130 and corresponds to
an
<code>
int
</code>
value.
</para>
<para>
If the expression
<code>
a + b
</code>
was of data type
<code>
byte
</code>
an arithmetic overflow as in the subsequent
code example would occur:
</para>
<programlisting
language=
"none"
>
byte a = 120, b = 10;
byte sum = (byte) (a + b);
System.out.println(sum);
</programlisting>
<para>
The explicit type conversion (a so called type cast or
cast for short) forces the 4-byte integer into a one-byte
variable
<code>
sum
</code>
thereby loosing the original value and
returning -126 instead.
</para>
</answer>
</answer>
</qandaentry>
</qandaentry>
</qandadiv>
</qandadiv>
...
...
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