What is a wrapper class? What is its use in JAVA?

Explain in brief the need of wrapper class and how it is useful.

  • Sachin Sarawgi
  • Sachin Sarawgi
  • |
  • 03 Jun
  • |
  • 33229 Views
  • |
  • 52 Answers

text limit upto 4000

  • 6

    Java is an object-oriented language and can view everything as an object. A simple file can be treated as an object , an address of a system can be seen as an object , an image can be treated as an object (with java.awt.Image) and a simple data type can be converted into an object (with wrapper classes). This tutorial discusses wrapper classes. Wrapper classes are used to convert any data type into an object.
    The primitive data types are not objects; they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language. For example, upto JDK1.4, the data structures accept only objects to store. A data type is to be converted into an object and then added to a Stack or Vector etc. For this conversion, the designers introduced wrapper classes.
    What are Wrapper classes?
    As the name says, a wrapper class wraps (encloses) around a data type and gives it an object appearance. Wherever, the data type is required as an object, this object can be used. Wrapper classes include methods to unwrap the object and give back the data type. It can be compared with a chocolate. The manufacturer wraps the chocolate with some foil or paper to prevent from pollution. The user takes the chocolate, removes and throws the wrapper and eats it.
    Observe the following conversion.
    int k = 100; Integer it1 = new Integer(k);
    The int data type k is converted into an object, it1 using Integer class. The it1 object can be used in Java programming wherever k is required an object.
    The following code can be used to unwrap (getting back int from Integer object) the object it1.
    int m = it1.intValue(); System.out.println(m*m); // prints 10000
    intValue() is a method of Integer class that returns an int data type.
    Importance of Wrapper classes
    There are mainly two uses with wrapper classes.
    1) To convert simple data types into objects, that is, to give object form to a data type; here constructors are used.
    2) To convert strings into data types (known as parsing operations), here methods of type parseXXX() are used.
    Features of the Java wrapper Classes.
    1) Wrapper classes convert numeric strings into numeric values.
    2) The way to store primitive data in an object.
    3) The valueOf() method is available in all wrapper classes except Character
    4) All wrapper classes have typeValue() method. This method returns the value of the object as its primitive type.

    • Voted By:
    • Shubham ,
    • Pratik ,
    • Madhav ,
    • Surbhi ,
    • Mohammad ,
    • Sujan

  • 1

    The wrapper class is used to do boxing and unboxing, it means that converting a primitive type to object type. Ex. int x=10; //it is a primitive data type. Integer i=new Integer(x); // it is converted as Integer object type

    • Voted By:
    • Elisetty

  • 0

    using wrapper classes we can make the primitive data types into objects Integer i = 20;(this is an object)


  • 0

    wrapper class in Java is given to achieve fully object oriented programming approach or feature. for every primitive data types they have given a special class.we can see wrapper class uses most of the time when you will work with collections.


  • 0

    Wrapper class In Java is used to convert one data type variable to it's equivalent variable of another data type For ex- we may read values from keyboard by using readLine() method of buffered reader class but it reads the input as string format so we need to convert it to integer type before we perform some operations (integer. ParseInt(object. readLine())


  • 0

    wrapper classes are introduced to reduce the drawback in primitive Data types.


  • 0

    Wrapper class makes the data type treated as object.we can make the data type object. I.e. Primitive data type:int Wrapper class: Int


  • 0

    Wrapper class contain constructor. Use:-For representing primitive data types into objects. Wrapper classes support static methods using which we can get back the primitive data type present in the string object back into its equivalent primitive data type. There are certain things in the program which accepts data only in the form of objects so the need of wrapper class comes


  • 0

    wrapper classes is a class whose object wraps or contains a primitive data type.when we create an object to a wrapper class,it contain a field and in this field,we can store a primitive data type in other words,we can wrap a primitive value into a wrapper class object . need of wrapper classes- 1) they convert primitive data type into object.objects are needed if we wish to modify the arguments passed into a method. 2)the classes in java.util package handles only objects and hence wrapper classes help in this case also. 3)data structure in the collection from work such as arraylist and vector ,store only objects and not primitive type. 4)an object is needed to support synchronization in multithreading. uses of wrapper classes in java - 1)to convert simple data type into objects. 2)to convert string into data type known as parsing operation.


  • 0

    It is a mechanism of having repeating of class one after another so to stop this we have been used wrapper class


  • 0

    Wrapper class is used to encapsulate other primitive class in Java. It , as the name suggests, its object wraps the primitive data. For example it's wraps int to Integer and double to Double and so on.


  • 0

    The term "wrapper class" in Java refers to the classes that represent the primitive types in Object form.


  • 0

     Wrapper classes convert numeric strings into numeric values.


  • 0

    interface having function to operate on primitive  data types


  • 0

    Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.


  • 0

    Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.


  • 0

    Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.


  • 0

    wrapper classes are used to convert primitive values to java objects i.e; wrapper objects .


  • 0

    Wrapper class are used to convert any data type into an object. The primitive data types are not objects,they do not belong to any class. Sometimes, it is required to convert data types into objects in Java


  • 0

    it means wrapps up around a datatype and gives it a object apperence. use-simple data type convert to objects.


Did Not Get The Answer? 
Contact people of Talent-Core Java directly from here