AppDividend
Latest Code Tutorials

Wrapper Class in Java Tutorial | Java Wrapper Classes Example

0

Wrapper Class in Java Tutorial | Java Wrapper Classes Example is today’s topic. A Wrapper class is the class whose object wraps or contains primitive data types. When we create the object to the wrapper class, it contains the field, and in this field, we can store primitive data types. In OOPs explanation we learned that object-oriented programming is all about objects and there are eight primitive data types (used as raw data for operations such as arithmetic, logical, etc.,), but they are not objects.

Wrapper Class in Java

A Wrapper class is a class in java which contains and wraps the primitive data types (char, short, bytes, etc.). In other words, wrapper classes provide a way to use primitive data types (int, char, etc..) as objects. This wrapper class comes under java.util package. All the wrapper classes (Integer, Long, etc.) are subclasses of the abstract class Number.

#Why we need Wrapper Class

  1. They convert primitive data types into objects. Objects are necessary if we wish to modify the arguments passed into the method (because primitive types are passed by value).
  2. The classes in java.util package handles only objects, and hence wrapper classes help in this case also.
  3. Data structures in the Collection framework, such as ArrayList and Vector, store only the objects (reference types) and not the primitive types.
  4. The object is needed to support synchronization in multithreading.
Primitive Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

Primitive Data types and their corresponding Wrapper Class

 

Wrapper Class in Java

What is the need for wrapper classes in java programming

For Example -While working with collections in Java, we use generics for type safety like this: ArrayList <Float> instead of this ArrayList <float>.

The Float is a wrapper class of float primitive type. We use wrapper class in this case because generics need objects, not primitives.

Wrapper class objects allow null values while primitive data type doesn’t allow null values. Helps in Multithreading because, in this, we need objects for synchronization. Used to convert the primitive data types into objects.

#Few utility methods of Wrapper classes

  1. valueOf(): Used to create Wrapper object for given primitive or String. 
  2. xxxValue(): Used to get the primitive for the given Wrapper Object. Here xxx can be byte, char, int etc(i.e., byteValue(), charValue()).
  3. toString(): Used to convert Wrapper object or primitive to String.

Since you’re now working with objects, you can use certain methods to get information about the specific object. You can see the use of some utility methods in the later implementations.

#Implementation of the wrapper class in Java

 

Implementation of the wrapper class in Java

#Autoboxing in Wrapper Class

In java programming, it is used to convert the primitive data types into corresponding objects.

For example, conversion of int to Integer, float to Float, etc. Let’s understand the above concept using the code given below. See the following code example.

// Java program to demonstrate Autoboxing 

class Auto
{  //declare class
   public static void main(String args[])
   {  

	//Converting int primitive into an Integer object  

	 int number=1;  //int primitive

	 Integer obj=Integer.valueOf(number); //creating Wrapper class (i.e.,Integer) object 

	 System.out.println(number+ " "+ obj);  
   }
}

See the following output.

 

Autoboxing in Wrapper Class

In the above example, you can see both primitive data type and object have the same values(as ‘1’). The conversion is done with the help of valueOf() method (used to create a Wrapper object for the given primitive type) You can use obj in place of number wherever you need to pass the value of a number as an object.

#Unboxing in Wrapper Class

In java programming, it is used to convert the Wrapper class object into corresponding primitive data types. For example conversion of Integer to int, Float to float, etc. Let’s understand the above concept using the code given below:

// Java program to demonstrate Unboxing

class Un
{  
   public static void main(String args[])
   {  
	
	 Integer obj = new Integer(1);  //Creating Wrapper class object (i.e., obj)
	
     int number = obj.intValue();//Converting the wrapper object to primitive(i.e., number)

	 System.out.println(number+ " "+ obj);  
   }
}

See the following output.

 

Unboxing in Wrapper Class

In the above example you can see how the wrapper object is first created (i.e., obj) then the object is converted to primitive data types (i.e., number) with the help of intValue () function (used to get the primitive for the given Wrapper Object).

#Keypoints of Wrapper Class

  1. The names of Wrapper Class beginning with a capital letter.
  2. All the wrapper classes are the subclass of the abstract class Number.
  3. It comes under java.util package.
  4. It is used to convert the wrapper class object to the primitive data type (Unboxing).
  5. It is also used to convert primitive data type into the wrapper class object (Autoboxing).

Finally, Wrapper Class in Java Tutorial | Java Wrapper Classes Example is over.

Leave A Reply

Your email address will not be published.

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