Java Type Wrappers

In Java, type wrappers are classes that encapsulate primitive data types, providing objects that can be used where an object is required. These wrapper classes are part of the java.lang package and are used to represent and manipulate primitive types in an object-oriented manner. The Java type wrappers include:

  1. Byte:
  • Wrapper for the byte primitive type.
  1. Short:
  • Wrapper for the short primitive type.
  1. Integer:
  • Wrapper for the int primitive type.
  1. Long:
  • Wrapper for the long primitive type.
  1. Float:
  • Wrapper for the float primitive type.
  1. Double:
  • Wrapper for the double primitive type.
  1. Character:
  • Wrapper for the char primitive type.
  1. Boolean:
  • Wrapper for the boolean primitive type.

Example Usage:

// Example of using Integer and Double wrappers
Integer intWrapper = new Integer(42); // Deprecated in Java 9
Integer anotherIntWrapper = Integer.valueOf(42); // Preferred way

int primitiveInt = intWrapper.intValue(); // Unboxing

Double doubleWrapper = 3.14;
double primitiveDouble = doubleWrapper; // Auto-unboxing

System.out.println("Integer value: " + intWrapper);
System.out.println("Double value: " + doubleWrapper);

Autoboxing and Auto-unboxing:

Java supports autoboxing (automatic conversion from primitive types to their wrapper classes) and auto-unboxing (automatic conversion from wrapper classes to primitive types).

// Autoboxing: converting primitive int to Integer wrapper class
Integer intWrapper = 42;

// Auto-unboxing: converting Integer wrapper class to primitive int
int primitiveInt = intWrapper;

Common Methods:

Each wrapper class provides methods for converting between primitive types and wrapper objects, as well as other utility methods. Here are some common methods:

  • valueOf(): Returns an instance of the wrapper class representing the specified value.
  • parseXxx(String s): Converts a string representation of a number to the corresponding primitive type (Xxx is the primitive type, e.g., parseInt() for int).
  • toString(): Returns a string representation of the object.

Use Cases:

  • Collections: Wrapper classes are often used in collections (e.g., List, Set, `Map), which store objects. Autoboxing allows you to add primitive values directly to collections.
List<Integer> integerList = new ArrayList<>();
integerList.add(42); // Autoboxing
int retrievedValue = integerList.get(0); // Auto-unboxing
  • Generic Classes: When working with generics, it’s common to use wrapper classes to represent generic numeric types.
class Container<T> {
    private T value;

    public Container(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }
}

Container<Integer> intContainer = new Container<>(42);
int retrievedValue = intContainer.getValue();

Using wrapper classes allows Java to treat primitive types as objects, providing additional functionality and compatibility with object-oriented concepts. However, it’s important to be mindful of the performance implications, especially in scenarios where large amounts of data are involved.

Java type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and Boolean. These classes offer a wide array of methods that allow us to fully integrate the primitive types into Java’s object hierarchy.

Character

Character is a wrapper around a char. The constructor for Character is

Character(char ch)

Boolean

Boolean is a wrapper around boolean values. It defines these constructors

Boolean(boolean boolValue)
Boolean(String boolString)

In the first version, boolValue must be either true or false. In the second version, if boolString contains the string “true” (in uppercase or lowercase), then the new Boolean object will be true. Otherwise, it will be false.

To obtain a boolean value from a Boolean object, use booleanValue( ),

boolean booleanValue( )

The Numeric Type Wrappers

The most commonly used type wrappers are those that represent numeric values. These are Byte, Short, Integer, Long, Float, and Double. All of the numeric type wrappers inherit the abstract class Number. Number declares methods that return the value of an object in each of the different number formats. These methods are

byte byteValue( )
double doubleValue( )
float floatValue( )
int intValue( )
long longValue( )
short shortValue( )

Demonstrate a type wrapper

class Wrap {
public static void main(String args[]) {
Integer iOb = new Integer(100);
int i = iOb.intValue();
System.out.println(i + " " + iOb); // displays 100 100
}
}

Leave a Comment