CSC300: Boxing [14/23] |
Conversion from base type to object type is called boxing
Conversion from object type to base type is called unboxing
For int
and double
, the operations are as follows
Base type | Object type | Boxing (base to object) | Unboxing (object to base) |
---|---|---|---|
int base = 0; |
Integer object = null; |
object = Integer.valueOf(base); |
base = object.intValue(); |
double base = 0.0; |
Double object = null; |
object = Double.valueOf(base); |
base = object.doubleValue(); |
Java has six additional base types:
Base type | Object type | Boxing (base to object) | Unboxing (object to base) |
---|---|---|---|
boolean base = false; |
Boolean object = null; |
object = Boolean.valueOf(base); |
base = object.booleanValue(); |
float base = 0.0F; |
Float object = null; |
object = Float.valueOf(base); |
base = object.floatValue(); |
byte base = 0; |
Byte object = null; |
object = Byte.valueOf(base); |
base = object.byteValue(); |
char base = 0; |
Character object = null; |
object = Character.valueOf(base); |
base = object.charValue(); |
short base = 0; |
Short object = null; |
object = Short.valueOf(base); |
base = object.shortValue(); |
long base = 0L; |
Long object = null; |
object = Long.valueOf(base); |
base = object.longValue(); |
Many languages make an explicit distinction between object and base types
struct
)