Answer to Question #128008 in Java | JSP | JSF for Yann

Question #128008
What are wrapper classes and why are they useful for ArrayLists? In your answer, include examples of autoboxing and unboxing.
1
Expert's answer
2020-08-02T15:25:41-0400

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);

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS