Introduction : 

  • Since java is an object-oriented programming language, and hence we need to deal with objects of primitive data types many times such as in Collections, Serialization, Synchronization etc problems.To solve these problems, we use wrapper class.
  • The wrapper classes are part of the java.lang package, which is imported by default (Not necessary to write) into all Java programs.

Definition : 

  • Wrapper classes is a class that provide a way to use primitive data types (byte, short, int, long, float, double, char and boolean) as objects and its vice – versa.
  • The automatic conversion of primitive data type into its equivalent object is known as autoboxing (for example – byte to Byte, char to Character, int to Integer, long to Long, float to Float, boolean to Boolean, double to Double, and short to Short)and its vice-versa is called unboxing(Since Java 5, we do not need to use the intValue() method of wrapper classes to convert the wrapper type into primitives).

Features : 

  • The eight classes of java.lang package are known as wrapper classes in java.
  • The wrapper objects hold much more memory compared to primitive types.So we use primitive data types when we need efficiency and use wrapper class object when we need objects instead of primitive types.
  • The primitive data types are not objects so they do not belong to any class.
  • While storing data in data structures, which support/receives only objects, then it is required to convert the primitive data type to object first.
  • The list of primitive data types and its equivalent wrapper classes are –

wrapper class image 1

    Primitive Data Types Wrapper Class
    byte Byte
    short Short
    int Integer
    long Long
    float Float
    double Double
    boolean Boolean
    char Character

    Advantage :

    • There are so many advantages of wrapper classes. They are – 
      • Wrapper class objects allow null values while primitive data type doesn’t allow it.
      • As we know that, java supports only call by value. So, when we pass a primitive data value to a function, it will not change the original value. But, if we convert the primitive data type in an object form, it will change the original value.
      • Serialization: We need to convert the objects into streams to perform the serialization. If we have a primitive value, we can convert it in objects through the wrapper classes.
      • Synchronization:  An object is needed to support synchronization in multithreading.
      • Collection Framework: Java collection framework works with objects only i.e.all classes of the collection framework (such as ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque  etc.) deal with objects only.

    Disadvantage : 

    • The wrapper objects hold much more memory compared to primitive types.

    Example  

    Example : A java program to convert primitive data type into wrapper object(boxing).

    class Example 
    { 
        public static void main(String[] arg) 
        {        
    		int num=100;
    		//Integer obj=Integer.valueOf(num);  //Old Pattern before java 5
                    Integer obj=num;    //New Pattern from java 5
    		System.out.println("The value in the form of Primitive type is =" + num );
    		System.out.println("The value in the form of wrapper object type is =" + obj);
        }
    }	
    Output :
    The value in the form of Primitive type is =100
    The value in the form of wrapper object type is =100

    Example : A java program to convert wrapper object into primitive data type(unboxing).

    class Example 
    { 
        public static void main(String[] arg) 
            {        
    		//Creating Wrapper class object 
    		    Integer obj = new Integer(100); 
    		//Converting the wrapper object to primitive type
    		    //int num = obj.intValue();   //Old Pattern before java 5
                        int num = obj;   //New Pattern from java 5
    		    System.out.println("The value in the form of Primitive type is =" + num );
    		    System.out.println("The value in the form of wrapper object type is =" + obj);
            }
    }
    The value in the form of Primitive type is =100
    The value in the form of wrapper object type is =100	

    Loading

    Categories: Java

    0 Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.