Type-Wrapper Classes for Primitive Types

Comprehensive study notes, diagrams, and exam preparation for Type-Wrapper Classes for Primitive Types.

Type-Wrapper Classes for Primitive Types

Definition

Type-Wrapper classes are specialized Java classes that encapsulate (or "wrap") a primitive data type within an object. While primitive types like int, double, and boolean provide high performance, they cannot be used in contexts where objects are required, such as in the Java Collections Framework. Wrapper classes provide the bridge between primitive values and object-oriented functionality.


Main Content

1. The Primitive vs. Object Dichotomy

  • Java maintains a strict distinction between primitive types (stored on the stack) and Objects (stored on the heap).
  • Wrapper classes (e.g., Integer, Double, Character) belong to the java.lang package and allow primitives to behave like objects.

2. The Relationship with Collections

  • The Java Collections Framework (List, Set, Map) is designed to store only Objects.
  • You cannot create a List<int> because int is not an object; instead, you use List<Integer>, which stores the wrapper objects.

3. Type-Wrapper Hierarchy

  • Every primitive has a corresponding Wrapper class: byte -> Byte, short -> Short, int -> Integer, long -> Long, float -> Float, double -> Double, char -> Character, and boolean -> Boolean.
  • All numeric wrapper classes inherit from the abstract class java.lang.Number.
[Object]
   |
[Number] (Abstract Class)
   |
   +-- Integer
   +-- Double
   +-- Long
   +-- Float

Working / Process

1. Boxing (Manual and Auto)

  • Boxing is the process of converting a primitive value into its corresponding wrapper object.
  • Manual Boxing: Integer obj = Integer.valueOf(10);
  • Autoboxing: Java automatically converts the primitive to an object: Integer obj = 10;

2. Unboxing (Manual and Auto)

  • Unboxing is the reverse process: extracting the primitive value from the wrapper object.
  • Manual Unboxing: int val = obj.intValue();
  • Auto-unboxing: Java automatically extracts the value: int val = obj;

3. Utility Methods for Data Conversion

  • Wrapper classes provide static methods to perform string-to-primitive conversions.
  • Example: int val = Integer.parseInt("123"); converts a String to an integer.
  • Example: String s = Integer.toString(val); converts an integer to a String.

Advantages / Applications

  • Collections Compatibility: Enables the use of primitives in ArrayList, HashMap, and other Collection types.
  • Utility Methods: Provides useful methods like MAX_VALUE, MIN_VALUE, parseInt(), and toBinaryString() to manipulate data.
  • Nullability: Unlike primitives which have default values (e.g., 0 for int), Wrapper classes can be null, which is useful for representing missing or optional data in database entities.

Summary

Type-Wrapper classes are Java objects that provide an object-oriented representation of primitive data types, allowing them to be utilized within the Java Collections Framework. Through the processes of autoboxing and unboxing, Java manages the conversion between primitives and their wrapper counterparts, while also offering essential utility methods for parsing and data manipulation.

Important terms to remember: Autoboxing, Unboxing, java.lang.Number, Primitive Types.