Boxing and Unboxing
Stack
of integer values
|
10 1 3 |
-
Supports searching of objects based on:
-
Objects only, no primitive types!
|
java.lang.Integer java.lang.Integer java.lang.Integer |
int iPrimitive ❶ = 7; Integer iInteger = ❷ iPrimitive; int iPrimitiveFromInteger = ❸ iInteger; |
int iPrimitive ❶ = 7; Integer iInteger = ❷ Integer.valueOf(iPrimitive); int iPrimitiveFromInteger = ❸ iInteger.intValue(); |
Boxing and unboxing | “Conventional” Java™ | ||||||
---|---|---|---|---|---|---|---|
|
|
|
No. 171
Auto boxing int to Double?
Q: |
Consider the following two code snippets:
Explain this result. Hint: You may want to read chapter 5 and section 5.2 in particular from the The Java® Language Specification. |
||||
A: |
“3.0” is a double literal. For the sake of clarification we may rewrite the working code snippet:
With autoboxing on offer the compiler will silently box the
value of type On contrary “3” is an
The “Boxing
Conversion” section does not define an An |