Showing posts with label Autoboxing/Unboxing. Show all posts
Showing posts with label Autoboxing/Unboxing. Show all posts

Tuesday, June 11, 2013

Java 1.5 Autoboxing/Unboxing

Integer i = new Integer(4);
int j = i.intValue();
Number n = new Float(3.14159);
 
Boolean stuff = new Boolean(false);
// stuff before ? must be a boolean (lower case)
System.out.println( stuff.booleanValue() ? "Yep" : "Nope" );
Sick of this? Me too. Do this instead:
Integer i = 4;
int j = i;
Number n = 3.14159f;
 
Boolean stuff = false;
System.out.println( stuff ? "Yep" : "Nope" );
This is pretty nice. Especially since you can use ++ and other similar operators with the wrapper types now too.

Read to continue : Java 1.5 Varargs