Java Garage

     

Here are the rules about it:

A value of a primitive type can be cast to another primitive type by identity conversion, if the types are the same, or by a widening primitive conversion or a narrowing primitive conversion.

A primitive type value can be cast to a reference type by boxing conversion.

int i = 5; //5 is a primitive int Integer iRef = i; // convert to wrapper //reference type: ok

A reference type value can be cast to a primitive type by unboxing conversion.

Integer iRef = new Integer(5); //starts life as reference type int j = i; //ok, unboxed to primitive

See the topic on Autoboxing for more about moving seamlessly between primitive and wrapper class types.

Категории