Wrapper classe are classes which convert primitive data types into objects. They a wrap primitive classes within them. For example Integer is wrapper class for int, Double is wrapper class for double.
Wrapper classes are useful for ArrayLists because ArrayList couldn't contain primitive classes directly. For example it is not possible to declare
ArrayList<int> arrayList = new ArrayList<int>();
But instead
ArrayList<Integer> arrayList = new ArrayList<Integer>();
Autoboxing is automatic conversion of primitive class object to wrapper class object. For example
ArrayList<Integer> arrayList = new ArrayList<Integer>();
int i = 5;
arrayList.add(i);
Unboxing is automatic conversion of wrapper class object to primitive class object. For example
ArrayList<Integer> arrayList = new ArrayList<Integer>();
int i = 5;
arrayList.add(i);
int j = arrayList.get(0);
Comments
Leave a comment