Primitive Types vs. Wrapper Classes

abhishek_kumar_d9009a7ae6

Abhishek Kumar

Posted on October 2, 2024

Primitive Types vs. Wrapper Classes

Java provides primitive data types and corresponding wrapper classes. Here's a breakdown of their differences and how they relate to autoboxing and unboxing.


Primitive Data Types

  • Definition: Primitive types are the most basic data types provided by Java. They hold simple values and are not objects.
  • Examples:
    • int (integer)
    • float (floating-point number)
    • char (character)
    • boolean (true/false)
    • Others: byte, short, long, double

Wrapper Classes

  • Definition: Wrapper classes are object representations of primitive types. Java provides a wrapper class for each primitive type.
  • Examples:
    • Integer (for int)
    • Float (for float)
    • Character (for char)
    • Boolean (for boolean)
    • Others: Byte, Short, Long, Double

Key Differences

Primitive Type Wrapper Class
Stores the actual value. Stores a reference to an object that contains the value.
Stored in the stack. Stored in the heap (since it is an object).
Cannot have null value. Can have a null value, since it’s an object.
More memory efficient. Consumes more memory (due to object overhead).
Cannot be used in collections like ArrayList. Can be used in collections, as they are objects.

Example:

int a = 5;          // Primitive
Integer b = new Integer(10);  // Wrapper class
Enter fullscreen mode Exit fullscreen mode

Autoboxing and Unboxing

Autoboxing and Unboxing refer to the automatic conversion between primitive types and their corresponding wrapper classes.

1. Autoboxing:

  • The process of automatically converting a primitive type into its corresponding wrapper class when needed.
  • Happens, for instance, when you try to add a primitive type to a collection (like ArrayList) that requires objects.

Example:

   int a = 10;
   Integer b = a;  // Autoboxing (int to Integer)
Enter fullscreen mode Exit fullscreen mode

2. Unboxing:

  • The reverse process where a wrapper class object is automatically converted to its corresponding primitive type.
  • Occurs, for example, when performing arithmetic operations with a wrapper class.

Example:

   Integer b = new Integer(20);
   int c = b;  // Unboxing (Integer to int)
Enter fullscreen mode Exit fullscreen mode

Autoboxing & Unboxing in Action:

Here’s how autoboxing and unboxing work when dealing with collections or simple operations.

Example 1: Using ArrayList

Since ArrayList can only hold objects, autoboxing allows adding primitives to it.

ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);  // Autoboxing from int to Integer
numbers.add(10); 

int sum = numbers.get(0) + numbers.get(1);  // Unboxing to int
System.out.println("Sum: " + sum);  // Output: Sum: 15
Enter fullscreen mode Exit fullscreen mode

Example 2: Arithmetic Operations

Integer x = 100;  // Autoboxing from int to Integer
Integer y = 200;

int result = x + y;  // Unboxing happens automatically here
System.out.println("Result: " + result);  // Output: Result: 300
Enter fullscreen mode Exit fullscreen mode

Key Points to Remember:

  1. Autoboxing/Unboxing simplifies coding by automatically converting between primitives and objects.
  2. Primitives are more memory-efficient and faster for basic operations, while wrapper classes provide additional functionality (e.g., handling null, storing in collections).
  3. Be cautious with null values when working with wrapper classes—unboxing a null value will throw a NullPointerException.

Practice Questions:

  1. Write a program that demonstrates autoboxing and unboxing with ArrayList and arithmetic operations.
  2. Can you explain why null is allowed for wrapper classes but not for primitives? Write a small code snippet where unboxing a null causes an exception.

Understanding the difference between primitive types and wrapper classes, along with autoboxing and unboxing, is crucial for optimizing memory and avoiding pitfalls like NullPointerException in Java!

💖 💪 🙅 🚩
abhishek_kumar_d9009a7ae6
Abhishek Kumar

Posted on October 2, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related