Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Understanding Wrapper Classes in Java

In this lesson, you will learn

  • Wrapper Class

 

Wrapper Classes

Wrapper classes in Java are a set of classes that provide a way to use primitive data types (such as int, char, boolean, etc.) as objects.

Since Java is an object-oriented language, there are situations where you need to work with objects rather than primitives, such as when using collections (e.g., ArrayList, HashMap), which only work with objects.

Wrapper classes allow you to convert primitives into objects and vice versa.

 

List of Wrapper Classes in Java

Each primitive data type in Java has a corresponding wrapper class:

Primitive Data Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

 

Key Features of Wrapper Classes

Key Features of Wrapper Classes

  1. Object Representation of Primitives:

    • Wrapper classes allow primitive data types to be used as objects. For example, you can store an int in an Integer object.

  2. Utility Methods:

    • Wrapper classes provide utility methods for converting between primitives and strings, parsing strings, and performing other operations. For example:

      • Integer.parseInt(String s) converts a string to an int.

      • Integer.toString(int i) converts an int to a string.

  3. Autoboxing and Unboxing:

    • Autoboxing: Automatic conversion of a primitive to its corresponding wrapper class object. For example:

      Integer num = 10; // Autoboxing: int to Integer
    • Unboxing: Automatic conversion of a wrapper class object to its corresponding primitive. For example:

      int num = new Integer(10); // Unboxing: Integer to int
  4. Immutability:

    • Wrapper classes are immutable, meaning their values cannot be changed once they are created. If you need a different value, you must create a new object.

  5. Use in Collections:

    • Collections like ArrayList, HashMap, etc., can only store objects. Wrapper classes allow primitives to be stored in these collections. For example:

      ArrayList<Integer> list = new ArrayList<>();
      list.add(5); // Autoboxing: int to Integer

 

Example Usage of Wrapper Classes

public class WrapperExample {
    public static void main(String[] args) {
        // Autoboxing: primitive to wrapper
        Integer num1 = 10; // int to Integer
        Double num2 = 5.5; // double to Double

        // Unboxing: wrapper to primitive
        int num3 = num1; // Integer to int
        double num4 = num2; // Double to double

        // Utility methods
        String str = "123";
        int num5 = Integer.parseInt(str); // String to int
        String str2 = Integer.toString(num5); // int to String

        // Using in collections
        ArrayList list = new ArrayList<>();
        list.add(10); // Autoboxing
        list.add(20);

        System.out.println("List: " + list);
        System.out.println("Parsed int: " + num5);
    }
}


Advantages of Wrapper Classes

  1. Compatibility with Object-Oriented Features:

    • Wrapper classes allow primitives to be used in contexts that require objects, such as collections, generics, and reflection.

  2. Utility Methods:

    • They provide useful methods for converting, parsing, and manipulating data.

  3. Nullability:

    • Wrapper classes can hold null values, whereas primitives cannot.

Disadvantages of Wrapper Classes

  1. Performance Overhead:

    • Wrapper classes introduce additional overhead due to object creation and garbage collection, which can impact performance in scenarios requiring high efficiency.

  2. Memory Usage:

    • Wrapper objects consume more memory than their primitive counterparts.

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review

 

 

Layer 1
Login Categories