1 / 6

Autoboxing

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 The primitive types are not classes (but something more primitive) No methods, no constructors, no inheritance, etc.

pilis
Download Presentation

Autoboxing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Autoboxing A new feature in Java 5.0

  2. 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

  3. 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. • Example • List myList = new ArrayList(); • myList.add(47); // illegal • myList.add(new Integer(47)); // explicit boxing • Int a = myList.get(0); // illegal • Integer aObject = myList.get(0); • Int a = aObject.intValue(); // explicit unboxing

  4. Autoboxing on 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

  5. When autoboxing falls short • 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 deferenced.

  6. Performance • Autoboxing works well with collections. • Don’t use a lot of autoboxing in fast-running aplications • scientific calculations, gaming, etc. • It takes too much time. • Try to stay with 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

More Related