Wrapper classes in Java

Wrapper classes are a set of classes that provide an object-oriented representation of primitive data types. They "wrap" or encapsulate primitive data types within objects, allowing them to be treated as objects in Java programs. Each primitive data type has a corresponding wrapper class in Java.

Java wrapper classes

  1. Boolean: Represents a boolean value (true or false).
  2. Byte: Represents a signed 8-bit integer value.
  3. Short: Represents a signed 16-bit integer value.
  4. Integer: Represents a signed 32-bit integer value.
  5. Long: Represents a signed 64-bit integer value.
  6. Float: Represents a single-precision 32-bit floating-point value.
  7. Double: Represents a double-precision 64-bit floating-point value.
  8. Character: Represents a single Unicode character.
  9. Void: Represents the absence of a value.

Wrapper classes are often used in situations where objects are required instead of primitive data types. For example, when working with collections such as ArrayList or LinkedList, which can only store objects, not primitives, the wrapper classes provide a way to wrap primitive values and store them in the collection.

Wrapper classes can be used to:
  1. Pass primitive data types to methods that require objects.
    For example, the Collections class has a method called sort() that takes an List object as its argument. If you want to sort a list of integers, you can wrap the integers in Integer objects and pass the List of Integer objects to the sort() method.
  2. Store primitive data types in collections.
    Most collections in Java can only store objects. You can wrap primitive data types in wrapper classes and store them in collections.
  3. Use primitive data types with reflection.
    Reflection is a feature of Java that allows you to inspect and manipulate the classes and members of a program at runtime. Wrapper classes can be used with reflection to access the methods and fields of primitive data types.

Wrapper classes also provide useful methods to perform operations on the wrapped values. For example, the Integer class provides methods for converting strings to integers, performing arithmetic operations, and converting integers to strings.

One important feature of wrapper classes is autoboxing and unboxing. Autoboxing allows automatic conversion between primitive types and their corresponding wrapper classes, while unboxing is the reverse process. This feature simplifies the code and allows seamless conversion between primitives and their wrapper counterparts.

int number = 10; // Wrap the primitive data type in a wrapper class Integer wrappedNumber = new Integer(number); // Use the wrapper class with a method that requires an object Collections.sort(new ArrayList<>(Arrays.asList(wrappedNumber)));

In this code, the number variable is a primitive data type. The wrappedNumber variable is a wrapper class that wraps the number variable. The Collections.sort() method takes an List object as its argument. The new ArrayList<>(Arrays.asList(wrappedNumber)) expression creates a List object that contains the wrappedNumber object. The Collections.sort() method is then used to sort the List object.

Wrapper classes are also utilized in Java's collection framework and various APIs that require objects instead of primitives. They provide a way to work with primitive data types in an object-oriented manner, enabling the use of object-oriented features like inheritance, polymorphism, and method overloading.


Wrapper classes in Java provide an object-oriented representation of primitive data types, allowing them to be treated as objects and providing useful methods and features for working with them.