Different Ways to Create the Instances of Wrapper Classes in Java
Last Updated :
18 Jul, 2022
Wrapper Class a class whose object wraps or contains primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object.
Methods:
We can use two ways to construct the instance of the Wrapper Classes
- Using the constructor of the wrapper class
- Using the valueOf() method provided by the Wrapper classes
- Using concept of AutoBoxing
Let us discuss both ways individually in detail
Method 1: Using the constructor of the wrapper class
Syntax:
ClassName object = new ClassName(argument);
Illustration:
Integer number = new Integer(5);
Method 2: Using the valueOf() method provided by the Wrapper classes
Syntax:
ClassName object = ClassName.valueOf(argument);
Illustration:
Integer number = Integer.valueOf(5);
Now the question arises what is the difference between the two methods in the creation of instances of the Wrapper classes and which method is better for constructing instances. Let us implement both of the methods to get fair play among them.
Implementation:
Example
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
Integer num1 = new Integer( 5 );
Integer num2 = new Integer( 5 );
Integer num3 = Integer.valueOf( 5 );
Integer num4 = Integer.valueOf( 5 );
boolean value1 = (num1 == num2);
boolean value2 = (num3 == num4);
System.out.println(value1);
System.out.println(value2);
}
}
|
Output:
false
true
Output explanation:
Note that, the instances of the classes point to the memory locations assigned in the heap and themselves do not hold the value. While we are creating objects with the help of the constructor of the wrapper class, each time a new memory is allocated in the heap and the objects point to the different memory locations. Hence, in the above example, In this case, both num1 and num2 are pointing to different memory locations, thus on the comparison, they return false.
Do note is not so in the case of the valueOf() method as the valueOf() method checks if any memory is allocated to the same value for that class in the heap. If it finds the value, then it provides the location of the previously allotted memory to the new instance and both start pointing to the same memory location in the heap. Hence, on the comparison, it returns true.
Since the wrapper class object’s values are immutable just like String and thus can not be changed once allotted, it does not affect how many instances are pointing to the same memory location. Hence, in the above example, the memory was allotted to value 5 and the num3 was pointing to that memory location, but when we created one more instance num4 with the same value, it also started pointing to the same memory location as pointed by num3.
Currently, the method using a constructor to create an instance is deprecated, and therefore it is always best to use the valueOf() method. So let us move ahead a bit discussing the new concept of autoboxing.
Method 3: Using the concept of AutoBoxing
AutoBoxing is to reduce the efforts to write the valueOf() method each time we are creating instances, AutoBoxing is implemented. The automatic conversion of primitive types to the object of their corresponding wrapper classes is known as AutoBoxing.
We were creating wrapper classes until now by using valueOf() method, but it seems quite lengthy when we can use AutoBoxing. In AutoBoxing, our work is done by the compiler, i.e. Java compiler in the background would perform the valueOf() operation and create the instance of it.
Instances created using autoboxing follow the process of valueOf() in the background and hence in this also, multiple instances with the same value point to the same memory location.
Illustration: In the above example, it can also be written as Integer.valueOf(15)) and put the reference of it in the object (i.e. a number).
Integer number = 15;
Syntax:
ClassName object = value;
// of primitive data type associated with the wrapper class.
Example:
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
Integer num1 = 5 ;
Integer num2 = 5 ;
boolean bool = (num1 == num2);
System.out.println(bool);
}
}
|
Output explanation:
Both the num1 and num2 are pointing to the same memory location in the heap as we discussed in the valueOf() method.
Similar Reads
How to Create Different Packages For Different Classes in Java?
Let us first know what is a class and package in Java. Class in java is a model for creating objects. It means that the properties and actions of the objects are written in class. Properties are represented by variables and actions of the objects are represented by methods. So, a class contains vari
6 min read
Difference Between Object and Instance in Java
The object is an instance of a class. A class is like a blueprint or template that defines the properties and behavior of objects. When we create an object we are creating an instance of that class. Object in JavaThe object is an instance of a class. A class is a blueprint or template that describes
3 min read
Different Types of Classes in Java with Examples
A class is a user-defined blueprint or prototype from which objects are created. Â It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order: Â Modifiers: A class can be public or has default access
11 min read
Difference Between Package and Interface in Java
In Java, packages and interfaces play crucial roles in organizing and structuring code. They serve different purposes and are used in distinct contexts. In this article, we will learn the concepts of the packages and interfaces in Java. syntax provides examples for each and then presents a table hig
2 min read
Need of Wrapper Classes in Java
Firstly the question that hits the programmers is when we have primitive data types then why does there arise a need for the concept of wrapper classes in java. It is because of the additional features being there in the Wrapper class over the primitive data types when it comes to usage. These metho
3 min read
Utility Methods of Wrapper Classes in Java
Prerequisite: Wrapper Classes The objective of the Wrapper class is to define several utility methods which are required for the primitive types. There are 4 utility methods for primitive type which is defined by the Wrapper class: 1. valueOf() method: We can use the valueOf() method to create a Wra
4 min read
Java | How to create your own Helper Class?
In Java, a Helper class is a class that contains useful methods which make common tasks easier, like error handling, checking input, etc. This class intends to give a quick implementation of basic functions so that the programmers do not have to implement them again and again. This class is easy to
7 min read
How to create a user defined javap tool?
What is a javap tool? The javap tool is used to get the information of any class or interface. The javap command (also known as the Java Disassembler) disassembles one or more class files. Its output depends on the options used ("-c" or "-verbose" for byte code and byte code along with innards info,
4 min read
Java Unnamed Classes and Instance Main Methods
Java has introduced a significant language enhancement in Java Enhancement Proposal (JEP) 445, titled "Unnamed Classes and Instance Main Methods". This proposal aims to address the needs of beginners, making Java more accessible and less intimidating. Let's delve into the specifics of this proposal
7 min read
How to Create Custom Class in Java?
Class is the collection of objects. Class is not a real-world entity it is just only templates and prototypes or blueprints. Class does not occupy memory. We can write a custom class as per our choice for an illustration purpose a sample is shown in the program below as a helper class. Example: Java
2 min read