What??
- All primitives in java have a corresponding wrapper classes, e.g.
intandInteger - Wrapper classes are reference types
Auto Boxing/Unboxing
Integer i = 5; // auto boxing
int j = i; // auto unboxing
// the compiler converts these to:
Integer i = Integer.valueOf(5);
int j = i.intValue();For what
So that we can make the primitives more general, e.g.
boolean find(Object[] items, Object target) {
for (Object item : items) {
if (item.equals(target)) {
return true;
}
}
return false;
}
// this works
Integer[] a = {1,2,3};
find(a, 2); // note that 2 is auto-boxed here
// this does not work, since int is not a subtype of Object
int[] a = {1,2,3};
find(a, 2);