Casting Between Primitives and Wrappers

     

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.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net