autoboxing a new feature in java 5.0. primitive types and classes in java we have primitive types...

6
Autoboxing A new feature in Java 5.0

Upload: samuel-jared-barber

Post on 05-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double

Autoboxing

A new feature in Java 5.0

Page 2: Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double

Primitive types and classes

• In Java we have primitive types– boolean, char, byte, short, int, long, float,

double– The primitive types are not classes (but

something more primitive)• No methods, no constructors, no inheritance, etc.

• Each primitive type has a wrapper class– Boolean, Character, Byte, Short, Integer,

Long, Float, Double

Page 3: Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double

In and out boxing in old Java• In old Java (before Java 5.0) you had to explicitly convert from

primitive type to wrapper, and vice versa– int → Integer: new Integer(i)

• Constructor in class Integer• Called “boxing”. The wrapper class is considered a “box”, and you are

putting the int into the box.– Integer → int: intValue()

• Method in class Integer• Called “unboxing”. You are taking the int out of the box.

• ExampleList myList = new ArrayList();myList.add(47); // illegalmyList.add(new Integer(47)); // explicit boxingInt a = myList.get(0); // illegalInteger aObject = (Integer)myList.get(0);Int a = aObject.intValue(); // explicit unboxing

Page 4: Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double

Autoboxing in Java 5.0

• In Java 5.0 we don’t need to explicitly convert from primitive type to wrapper type.

• Example– List<Integer> myList = new ArrayList<Integer>();– myList.add(47); // legal: 47 is automatically boxed– int a = myList.get(0); // legal: a is automatically

unboxed• Result

– Fewer lines– Cleaner code

Page 5: Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double

When autoboxing doesn’t work

• Integer can have a null value, int cannot– Unboxing may result in a NullPointerException

• Integer a = null;• int res = 45 + a; // throws NullPointerException

• == has different semantics (meaning)– int b=3, c=3;– Integer d=new Integer(3), e = new Integer(3);– b == c true– d == e false– d.equals(e) true– b.equals(c)

• Does not compile: b is primitive and cannot be de-referenced.

Page 6: Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double

Performance• Autoboxing works well with collections.• Don’t use a lot of autoboxing in fast-running applications

– scientific calculations, games, etc.– It takes too much time.– Use the primitive types

• They are faster than the wrappers.– Don’t do this at home (in a fast-running application)

• Integer a, b;• Integer result = a + b;

– Unboxes a and b. Executes plus. Boxes result.– This is what you should do

• int a, b;• int result = a + b;

– No autoboxing