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
b83b7307
Commit
b83b7307
authored
8 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Correcting int to short assignment explanation
parent
422408d7
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
+20
-20
20 additions, 20 deletions
Doc/Sd1/languageFundamentals.xml
with
20 additions
and
20 deletions
Doc/Sd1/languageFundamentals.xml
+
20
−
20
View file @
b83b7307
...
...
@@ -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
prece
e
ding
<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 t
o -32768).
</para>
</inlineequation>
(o
r
-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"
>
...
...
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