Wrapper class in Java


Java Wrapper

Java Wrapper class

Wrapper class in Java converts or wraps primitive data types as objects. This means we can convert primitive values into objects and vice versa. There are 8 primitive data types which have an equivalent wrapper class. These wrapper classes extend the Number class which is the parent class.

Primitive Data typeWrapper class
booleanBoolean
byteByte
charChar
doubleDouble
floatFloat
intInteger
longLong
shortShort

Wrapper class in Java

Use of Wrapper class in Java

Below are the uses or need of a wrapper class in Java:

  • A wrapper class is mainly used in Collection classes like LinkedList, ArrayList, etc which stores only objects and not primitive values.
  • The java.util package can use only objects and hence wrapper class is useful in this situation.
  • Supports multithreading synchronization
  • Allows storing null values.
  • We can change values if we pass as an object in the form of a wrapper class. This is not possible for primitive types.

Creating a wrapper object

We can create a wrapper class object for a primitive data type in the below way. We need to specify the Wrapper class instead of the data type. For example, use Integer instead of int and Character instead of char.

public class AutoboxingDemo {

  public static void main(String[] args) {
    Boolean bln = true;
    Byte b = 4;
    Character c = 's';
    Double d = 100.5;
    Float f= 2.5f;
    Integer i = 10;
    
    
    System.out.println("Integer: " + i);
    System.out.println("Character: " + c);
    System.out.println("Boolean :" + bln);
    System.out.println("Byte: " + b);
    System.out.println("Double: "  + d);
    System.out.println("Float: " + f);

  }

}
Integer: 10
Character: s
Boolean :true
Byte: 4
Double: 100.5
Float: 2.5

Autoboxing

Autoboxing automatically converts primitive values into their respective wrapper classes. For example, we can convert int to Integer, etc.

public class AutoboxingDemo {

  public static void main(String[] args) {
    
    int i = 50;
    Integer it = i;
    System.out.println("Integer: " + it);
    
    char c = 'j';
    Character ch = c;
    System.out.println("Character: " + ch);

  }

}
Integer: 50
Character: j

Unboxing

Unboxing is the reverse process of autoboxing in which we can automatically convert objects of wrapper class into its corresponding primitive data types.

import java.util.ArrayList;

public class UnboxingDemo {

  public static void main(String[] args) {
    //Unboxing Double into double
    Double d = 50.5;
    double db = d;
    System.out.println("double: " + db);
    
    ArrayList<Integer> al = new ArrayList<Integer>();
    al.add(100);
    
    //Unboxing Integer into int
    int a = al.get(0);
    System.out.println("int: " + a);

  }

}
double: 50.5
int: 100

Methods supported by the wrapper classes

MethodDescription
byte ByteValue()
short ShortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()
Returns the value of this Number object into its corresponding primitive type
int compareTo(Byte byte)
int compareTo(Double double)
int compareTo(Float float)
int compareTo(Long long)
int compareTo(Integer int)
int compareTo(Short short)
Compares this Number object with the specified argument
Integer valueOf(int i)Returns the Integer object of the specified int primitive value.
String toString()Returns a string representation of the specified value.
See also
CyclicBarrier in Java

The below example demonstrates a few methods which are supported by the wrapper class in java.

public class AutoboxingDemo {

  public static void main(String[] args) {
    
    Integer i = Integer.valueOf(30);
    Character c = Character.valueOf('g');

    System.out.println(i.intValue());
    System.out.println(c.charValue());
    
    
  }

}
30
g

Custom wrapper class

We can create a custom wrapper class by wrapping a primitive data type. Let’s discuss this with an example below. In this example, we are creating a wrapper for an Integer data type. The toString() method returns the string representation of the integer value. We can retrieve and set the integer value using the getValue() and setValue() method.

class Wrapper{
  private int it;
  
  Wrapper(int i){
    this.it = i;
  }
  
  public int getValue() {
    return it;
  }
  
  public void setValue(int i) {
    this.it = i;
  }
  
  public String toString() {
    return Integer.toString(it);
  }
}
public class CustomWrapperDemo {

  public static void main(String[] args) {
    Wrapper w = new Wrapper(30);
    w.setValue(50);
    System.out.println(w.getValue());
    System.out.println(w);

  }

}
30
50

 

Reference