Java

Wrapper Class – Java

We discovered that objects are the foundation of object-oriented programming from the OOPs principles guide. In order to turn the eight basic data types—byte, short, int, long, float, double, char, and boolean—into objects, such as int to Integer, double to Double, float to Float, and so on—Wrapper classes are employed. Take a look at this straightforward illustration to see why Java requires wrapper classes.

Java developers, for instance, employ generics to provide type safety when dealing with collections, such as this: ArrayListInteger> rather than this ArrayListint>. A wrapper class for the int primitive type, integer. Because generics need objects rather than primitives, we use a wrapper class in this situation. We will also explore some more factors that can make you choose a wrapper class over a basic type in this article. To practice writing Java applications, click over to the section devoted to examples.

Primitive Data Type Corresponding Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

Why we need wrapper class in Java

1. As I already indicated, using wrappers with the collections API is one of the reasons we need them. However, compared to basic types, wrapper objects have a lot more memory available. So, instead of using basic types when you require efficiency, use wrapper classes when you need objects.

The basic data types do not belong to any class since they are not objects. It is necessary to change the primitive type to an object first when storing in data structures that only allow objects, which we may achieve by utilizing wrapper classes.

Example:

HashMap<Integer, String> hm = new HashMap<Integer, String>();

Wrapper classes are thus used for type safety. By doing this, we guarantee that the keys in this hash map are of the integer type and the values are of the string type.
2. Objects of the wrapper class are capable of holding null values, unlike basic data types.

Let’s examine a couple instances to better comprehend how conversion functions:

Wrapper Class Example 1: Converting a primitive type to Wrapper object

public class JavaExample{  
   public static void main(String args[]){  
	//Converting int primitive into Integer object  
	int num=100;  
	Integer obj=Integer.valueOf(num);  

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

Output:

100 100

As you can see, both the object and the primitive data type contain the identical values. Anywhere you need to supply the value of num as an object, you may substitute obj for num. This subject is explained in full at Autoboxing and Unboxing in Java. Autoboxing is the conversion of a primitive data type to an object, while unboxing is the conversion of an object back to a basic data type.

Wrapper Class Example 2: Converting Wrapper class object to Primitive

public class JavaExample{  
   public static void main(String args[]){  
	//Creating Wrapper class object 
	Integer obj = new Integer(100);  
		
	//Converting the wrapper object to primitive
	int num = obj.intValue();

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

Output:

100 100

Custom Wrapper Class

A unique wrapper class may be made to convert a primitive type into an object. Here, we have an int data type that is a member of the XYZ class. The constructor and getter setter methods of the XYZ class may be used to utilize this basic data type as an object, as seen below:

class XYZ{
  private int num;
  //default constructor
  XYZ(){}
  //parameterized constructor
  XYZ(int num){
    this.num=num;
  }
  //getter and setter methods
  public int getIntValue(){
    return num;
  }
  public void setIntValue(int i){
    this.num=i;
  }
  @Override
  public String toString() {
    return Integer.toString(num);
  }
}
public class JavaExample{
  public static void main(String[] args){
    XYZ obj = new XYZ(10);
    System.out.println(obj);
    obj.setIntValue(100);
    System.out.println(obj.getIntValue());
  }
}

Output:

10
100

Conclusion

We discovered the benefits of objects over basic data types in this tutorial. How to use a wrapper class to convert primitive types to objects. We also learnt when to utilize objects over basic types. Visit the Java Tutorial section if you wish to learn more about these Java-related subjects.

Related Articles

Leave a Reply

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

Back to top button