Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Understanding Java Wrapper Classes: Types, Conversions, and Benefits

Java Wrapper Class

In this tutorial, we will learn about the Java Wrapper class with the help of examples.

Java wrapper classes convert primitive types (int, char, float, etc.) into their corresponding object representations.

Each of the eight primitive types has a dedicated wrapper class.

Primitive TypeWrapper Class
byteByte
booleanBoolean
charCharacter
doubleDouble
floatFloat
intInteger
longLong
shortShort

Convert Primitive Type to Wrapper Objects

Use the valueOf() method to explicitly transform a primitive into its wrapper object.

Example 1: Primitive Types to Wrapper Objects

class Main {
  public static void main(String[] args) {
    int a = 5;
    double b = 5.65;
    Integer aObj = Integer.valueOf(a);
    Double bObj = Double.valueOf(b);
    if(aObj instanceof Integer) {
      System.out.println("An object of Integer is created.");
    }
    if(bObj instanceof Double) {
      System.out.println("An object of Double is created.");
    }
  }
}

Output

An object of Integer is created.
An object of Double is created.

The code above demonstrates explicit conversion via valueOf(). Java also supports auto‑boxing, where primitives are implicitly wrapped:

int a = 5;
Integer aObj = a;

double b = 5.6;
Double bObj = b;

For more on autoboxing and unboxing, see our dedicated guide.

Note: Prior to Java 9, wrapper objects could also be created via constructors (e.g., new Integer(a)), but this approach is deprecated.


Wrapper Objects into Primitive Types

To retrieve the underlying primitive, call the corresponding …Value() method on the wrapper object.

Example 2: Wrapper Objects into Primitive Types

class Main {
  public static void main(String[] args) {
    Integer aObj = Integer.valueOf(23);
    Double bObj = Double.valueOf(5.55);
    int a = aObj.intValue();
    double b = bObj.doubleValue();
    System.out.println("The value of a: " + a);
    System.out.println("The value of b: " + b);
  }
}

Output

The value of a: 23
The value of b: 5.55

Java also performs unboxing automatically:

Integer aObj = Integer.valueOf(2);
int a = aObj;

Double bObj = Double.valueOf(5.55);
double b = bObj;

See the autoboxing and unboxing article for deeper insights.


Advantages of Wrapper Classes

Note: Primitive types are generally more efficient than their object counterparts; use primitives when performance is critical.


Java

  1. Understanding Java’s final Keyword: Variables, Methods, and Classes
  2. Java instanceof Operator: A Comprehensive Guide
  3. Mastering Java Anonymous Inner Classes: Definition, Syntax, and Practical Examples
  4. Mastering Java’s ObjectInputStream: A Comprehensive Guide
  5. Mastering Java ObjectOutputStream: Serialization, Methods, and Practical Examples
  6. Mastering Java’s PrintStream Class: Print, Println, and Printf Explained
  7. Mastering Java Reader Class: Subclasses, Methods, and Practical Example
  8. Mastering Java Generics – Building Reusable, Type‑Safe Code
  9. Mastering Java File Operations with java.io – Creation, Reading, Writing & Deletion
  10. Understanding Java Wrapper Classes: Types, Conversions, and Benefits