Search

Friday, April 1, 2011

Rules to Remember - Boxing Java 5


package SCJP;

/** Rules to Remember :
 *
 * Auto boxing and unboxing  allow to implicit cast
 * in between wrapper classes and those primitive types
 * 1. Primitive to Object - Auto Boxing
 * 2. Object to Primitive - Un Boxing
 * 3. Null Pointer in case if null object assign to primitive - care full
 */
import static java.lang.System.out;

public class boxingDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 10;
Integer WrapperInt = i; // 1. Autoboxing  primitive assign to object
out.println(WrapperInt);


Float Wrapf = new Float("34.78");
float primf = 56.78f; //2. f suffix required for float
primf = Wrapf; // Unboxing

out.println(primf);

Integer j = null;
int k = j; //3.  NullPointer exception while assigning null object to primitive

}

}

No comments:

Post a Comment