Different Ways to Create the Instances of Wrapper Classes in Java


To encapsulate or represent a primitive datatype within an object, Java provides theconcept of Wrapper classes. All eight wrapper classes are as follows Double, Float, Long,Integer, Short, Byte, Character, and Boolean. These classes have a variety of in-builtmethods that allows us to integrate primitives into their respective instances as well asdisintegrate instances into their respective primitive datatypes. This article aims to explainthe different ways to create instances of Wrapper Classes.

Creating Instances of Wrapper Classes

The following approaches are available in Java to create Instances of Wrapper Classes −

With the help of Constructor

Although we can create an instance of wrapper class using its constructor, it is notrecommended as with the release of 9th version of JDK, this approach is deprecated andmarked for removal. The reason is whenever we create an instance using the constructor,a new memory gets allocated in the heap. It contradicts the immutability of instances ofthe wrapper classes.

Syntax

nameOfClass nameOfObject = new nameOfClass( value );

Here, value is the element that will get stored in the constructor

Instance

Integer val1 = new Integer(540);

With the help of Class Name

We can also create an instance of wrapper class without using the new keyword or constructor. This approach is also called Autoboxing as the conversion from primitives to their corresponding classes is done automatically by the Java Compiler. This way of creating instances is the same as the declaration and initialization of primitive datatypes.

Syntax

nameOfClass nameOfObject = value;

Example

The following example illustrates the creation of instances using the name of wrapper class.

import java.util.*;
public class Wrapp {
   public static void main(String args[]) {
      Integer val1 = 540;
      Double val2 = 66.456;
      Character val3 = 'T';
      Boolean val4 = true;
      Float val5 = 12.1f;
      System.out.println("The value of wrapper class instances are: ");
      System.out.println(val1 + ", " + val2 + ", " + val3 + ", " + val4 + "," + val5);
   }
}

Output

The value of wrapper class instances are:
540, 66.456, T, true, 12.1

With the help of valueOf Method

It is a static method that takes one or two arguments according to the needs andencapsulates the primitive type into corresponding wrapper class object.

Syntax

nameOfClass nameOfObject = nameOfClass.valueOf( value );

Example

The following example illustrates the creation of instances using the name of wrapper class.

import java.util.*;
public class Wrapp {
   public static void main(String args[]) {
      Integer val1 = 540;
      Double val2 = 66.456;
      Character val3 = 'T';
      Boolean val4 = true;
      Float val5 = 12.1f;
      System.out.println("The value of wrapper class instances are: ");
      System.out.println(val1 + ", " + val2 + ", " + val3 + ", " + val4 + ", " + val5);
   }
}

Output

The value of wrapper class instances are:
540, 66.456, T, true, 12.1

Conclusion

We started this article by defining the wrapper classes and their types. In the next section,we discussed three approaches to creating instances of wrapper class. Creating instancesusing the constructor is deprecated from the 9th JDK version. Therefore, we use eitherautoboxing or valueOf method to do so.

Updated on: 20-Jul-2023

139 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements