1.

Why do we need Wrapper classes in Java?

Answer»

A WRAPPER class is a specific class whose object CONTAINS or WRAPS PRIMITIVE data types. It can also be said that primitive data types are wrapped into an object of a wrapper class.

Some of the reasons the wrapper classes are required in Java are given as follows:

  1. The wrapper classes convert the primitive data types into objects. This is required if the arguments passed to the methods need to be modified as the primitive data types are passed by value.
  2. Synchronization in multithreading is supported by objects and so wrapper classes may be required.
  3. Only objects and not primitive types are stored in data structures such as ArrayList and Vector in the Collection framework.
  4. Another use of wrapper classes is because the classes in the java.util package HANDLE only objects.

A list of all the primitive data types and the corresponding wrapper classes is given as follows:

Primitive Data Type
Wrapper Class
Char
Character
Byte
Byte
Long
Integer
Short
Short
float
Float
Double
Double
Boolean
Boolean


Discussion

No Comment Found