top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are wrapper classes?

+2 votes
631 views
What are wrapper classes?
posted Mar 18, 2014 by Ashwini Miraj

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

As the name says, a wrapper class wraps (encloses) around a data type and gives it an object appearance.
wrapper class ex: int is a data type Integer is a wrapper class

take one ex:
int k = 100;
Integer it1 = new Integer(k);The int data type k is converted into an object, converted into object type.
Primitive data type Wrapper class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

retrive from integer object to int is:
int m = it1.intValue();
Importance of Wrapper classes

There are mainly two uses with wrapper classes.

To convert simple data types into objects, that is, to give object form to a data type; here constructors are used.
To convert strings into data types (known as parsing operations), here methods of type parseXXX() are used.

answer Mar 18, 2014 by Anand Kalagi
0 votes

Wrapper classes are classes that are used to make primitive variables into objects, and to make wrapped objects into primitives. int, boolean, double are all primitive data types and their respective wrapper classes are Integer, Boolean, and Double.

Wrapper classes are useful in storing primitive data types in higher level data structures such as Stack, List, Queue, since primitives cannot be directly placed in these data structures they must be boxed in the wrapper classes. But, here's the good news, with the new Java 5.0, there is no need no worry about wrapper classes and boxing and unboxing (unless it's something taught in class), since there is auto-boxing and unboxing, therefore, one can directly "add" primitives to a data structure and let the JVM do the rest.

answer Mar 18, 2014 by Salil Agrawal
...