Question

In: Computer Science

What are wrapper classes and why are they useful for ArrayLists? In your answer, include examples...

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.

Solutions

Expert Solution

Wrapper classes:

  • Wrapper classes are the part of Java programing language.
  • Wrapper classes are basically used for to hold one primitive data ( primitive data types are those data types which is passed by value ) type value.
  • Example: byte , short , int , long , float , double , boolean and char.
  • Primitive type  Wrapper class
    boolean         Boolean
    byte            Byte
    char            Character
    float           Float
    int             Integer
    long            Long
    short           Short
    double          Double
  • Wrapper classes object hold one value of their corresponding primitive type.
  • Primitive value is stored in a field inside the object.
  • This type of classes are used when we want store primitive data types in Java progaming structures that require objects like - Arraylists.
  • Wrapper classes may be used with the help of Autoboxing and unboxing.
  • Wrapper classes are used to provide the parse methods.
  • It is also used for constants such as MAX_VALUE, MIN_VALUE.
  • By the help of wrapper classes we can modify the arguments.

They are useful for ArrayLists because:

  • ArrayLists cannot hold primitive data types such as int, double, char, and long hence for these reason we use wrapper classes.
  • ArrayLists store only objects which is non primitive data types.
  • ArrayLists element are accessed by same as array by index value.
  • The size of a ArrayLists can grow or shrink as we need by adding and removing items after the ArrayLists has been created.

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:

  • It is reverse process of autoboxing.
  • It is used to convert an object of a wrapper class to its corresponding primitive type is known as 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);
    }
}

Related Solutions

A)   What are wrapper Classes? What are the available wrapper classes? B) What is Autoboxing and...
A)   What are wrapper Classes? What are the available wrapper classes? B) What is Autoboxing and Unboxing?
Why do you think Java provides both primitive data types and wrapper classes for them? Why...
Why do you think Java provides both primitive data types and wrapper classes for them? Why not just one or the other?
Give examples showing how "super" and "this" are useful with inheritance in Java. Include examples of...
Give examples showing how "super" and "this" are useful with inheritance in Java. Include examples of using "super" and "this" both as constructors and as variables.
Give examples showing how "super" and "this" are useful with inheritance in Java. **Include examples of...
Give examples showing how "super" and "this" are useful with inheritance in Java. **Include examples of using "super" and "this" both as constructors and as variables.**
What is the difference between disabilities and handicaps? Include in your answer why it is important...
What is the difference between disabilities and handicaps? Include in your answer why it is important to avoid "labels" when speaking about these individuals. What does intelligence mean? Include in your answer the differences between general, fluid and crystallized intelligence. What is the theory of multiple intelligence? Hint: Your answer cannot be considered complete without mentioning Howard Gardner and the eight intelligences. What is the triarchic theory of successful intelligence? Hint: Your answer cannot be considered complete without mentioning Robert...
a.)In your opinion, who is the greatest beneficiary of the audit and why? (Examples may include...
a.)In your opinion, who is the greatest beneficiary of the audit and why? (Examples may include investors, creditors, the company itself, potential employees, etc.) b.)Can the quality of an audit always be assured? Why or why not? What might interfere with audit quality? c.)Do you agree with the notion that there are factors outside of the auditor’s control that affect the quality of the auditor’s work, and why or why not? Is that acceptable? Should that be made clearer to...
C# Answer in discussion format Why is an array such a useful tool? What is a...
C# Answer in discussion format Why is an array such a useful tool? What is a jagged array? Ask your students to describe some circumstances in which jagged arrays might be useful. the System.Array class contains a variety of useful, built-in methods, describe one of them and how you would use it. Describe how to resize an array in C# using the new operator. How would you declare an array if you do not know in advance how many elements...
Should a researcher adjust the process to include cultural differences? Give examples to support your answer
Should a researcher adjust the process to include cultural differences? Give examples to support your answer
Be sure your answer is complete and include any relevant diagrams and examples. 3 paragraphs minimum....
Be sure your answer is complete and include any relevant diagrams and examples. 3 paragraphs minimum. Deciding to commit a crime is a rational decision. Explain why we make this statement. What benefits and what costs do a criminal incur when committing a crime? Using this concept of rationality, what policies can be used to reduce the amount of crime?
Why is taxonomy important and useful? Include a brief description of the three Cs.
Why is taxonomy important and useful? Include a brief description of the three Cs.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT