In: Computer Science
What are wrapper classes and why are they useful for ArrayLists? In your answer, include examples of autoboxing and unboxing.
Your Discussion should be at least 250 words in length, but not more than 750 words. Once you’ve completed your initial post, be sure to respond to the posts of at least 3 of your classmates.
Wrapper classes:
Primitive type Wrapper class boolean Boolean byte Byte char Character float Float int Integer long Long short Short double Double
They are useful for ArrayLists because:
AutoBoxing: It is used for automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing.
Example 1:- Let us we write: Integer m = 6;
Java code : Integer m = new Integer(6);
Example 2:- ArrayList runs = new ArrayList(); runs.add(9);
int r = runs.get(0);
Example 3:-
class Autoboxing
{
public static void mySolution(Integer num){
System.out.println(num);
}
public static void main(String[] args) {
mySolution(10);
}
}
Unboxing:
class Unboxing
{
public static void mySolution(int num){
System.out.println(num);
}
public static void main(String[] args) {
Integer inum = new Integer(107);
mySolution(inum);
}
}