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
e9036454
Commit
e9036454
authored
5 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Better boxing failure explanation
parent
9294cbc2
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/workingWithNumbers.xml
+43
-10
43 additions, 10 deletions
Doc/Sd1/workingWithNumbers.xml
with
43 additions
and
10 deletions
Doc/Sd1/workingWithNumbers.xml
+
43
−
10
View file @
e9036454
...
...
@@ -246,16 +246,23 @@ while (!values.empty()) {
<qandadiv>
<qandaentry>
<question>
<para>
Consider the following
boxing conversion
:
</para>
<para>
Consider the following
two code snippets
:
</para>
<programlisting
language=
"java"
>
double d = 3.0;
Double dInstance = d;
</programlisting>
<informaltable
border=
"0"
>
<tr>
<td
valign=
"top"
><programlisting
language=
"java"
>
Double d = 3.0;
</programlisting></td>
<para>
This code compiles and executes perfectly well. On contrary
the following snippet does not:
</para
>
<td
valign=
"top"
><programlisting
language=
"java"
>
Double d = 3;
</programlisting></td>
</tr
>
<programlisting
language=
"java"
>
int i = 3;
Double dInstance = i;
</programlisting>
<tr>
<td
valign=
"top"
>
o.K.
</td>
<td
valign=
"top"
><para>
Compile time error:
</para><screen>
Incompatible types.
Required: java.lang.Double
Found: int
</screen></td>
</tr>
</informaltable>
<para>
Explain this result. Hint: You may want to read
<link
xlink:href=
"https://docs.oracle.com/javase/specs/jls/se12/html/jls-5.html"
>
chapter
...
...
@@ -267,9 +274,35 @@ Double dInstance = i;</programlisting>
</question>
<answer>
<para>
The compiler will not perform auto boxing from
<code>
int
</code>
do
<code>
Double
</code>
. On the other hand Double
is no subtype of Integer disallowing a widening reference
<para><quote>
3.0
</quote>
is a double literal. For the sake of
clarification we may rewrite the working code snippet:
</para>
<programlisting
language=
"java"
>
double doubleValue = 3.0;
Double d = doubleValue;
</programlisting>
<para>
With autoboxing on offer the compiler will silently box the
value of type
<code
language=
"java"
>
double
</code>
into a
corresponding instance of type
<classname>
Double
</classname>
.
</para>
<para>
On contrary
<quote>
3
</quote>
is an
<code
language=
"java"
>
int
</code>
literal allowing for re-writing the
second snippet as:
</para>
<programlisting
language=
"java"
>
int intValue = 3.0;
Double d = intValue;
</programlisting>
<para>
The
<quote
xlink:href=
"https://docs.oracle.com/javase/specs/jls/se12/html/jls-5.html#jls-5.1.7"
>
Boxing
Conversion
</quote>
section does not define an
<code
language=
"java"
>
int
</code>
to
<classname>
Double
</classname>
boxing
conversion: The compiler will thus not perform auto boxing from
<code>
int
</code>
do
<classname>
Double
</classname>
.
</para>
<para>
An
<code
language=
"java"
>
int
</code>
could however be
auto-boxed into an
<classname>
Integer
</classname>
. But
<classname>
Double
</classname>
not being a subtype of
<classname>
Integer
</classname>
disallows a widening reference
conversion.
</para>
</answer>
</qandaentry>
...
...
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