Java Wrapper Class

Java Wrapper Class

What is Java Wrapper Class?

  • In Java, a wrapper class is a class that wraps around a primitive data type, allowing it to be treated as an object. The wrapper class provides a way to convert primitive data types to objects and vice versa.
  • Wrapper classes are used when we need to treat a primitive data type as an object.
    For example, some Java libraries and APIs require that data be passed as objects rather than primitive data types.
  • To understand the Java Wrapper Class, Read the Complete Article.

Advantages of Java Wrapper Class:

Wrapper classes in Java provide several advantages over primitive data types. Here are some of the advantages:

  • Object-oriented features: Wrapper classes provide object-oriented features that are not available in primitive data types. For example, they allow us to use methods to manipulate data, and to pass data as parameters to methods.
  • Null values: Primitive data types cannot be assigned a null value, whereas wrapper classes can. This is useful in situations where a variable may or may not have a value, such as when working with databases.
  • Generics: Wrapper classes can be used with generics, which is not possible with primitive data types. This allows for more flexibility in working with data structures and collections.
  • Type conversion: Wrapper classes provide methods for converting data between different types. For example, the Integer class provides methods for converting a string to an integer, or an integer to a string.
  • Interoperability: Wrapper classes provide a way to interact with code that requires objects, such as APIs and libraries.

Overall, wrapper classes provide a more flexible and powerful way to work with data in Java, and allow for more advanced programming techniques.

Java Wrapper Class

Let’s look at the Java Wrapper Class to perform certain operations.

Example 1: Convert Primitive Type to Wrapper Objects

Run
public class Main {
    public static void main(String[] args) {
        int num = 79;
        Integer wrappedNum = Integer.valueOf(num);
        System.out.println("Primitive int: " + num);
        System.out.println("Wrapped Integer: " + wrappedNum);

        double pi = 3.14;
        Double wrappedPi = Double.valueOf(pi);
        System.out.println("Primitive double: " + pi);
        System.out.println("Wrapped Double: " + wrappedPi);

        char letter = 'a';
        Character wrappedLetter = Character.valueOf(letter);
        System.out.println("Primitive char: " + letter);
        System.out.println("Wrapped Character: " + wrappedLetter);
    }
}

Output

Primitive int: 79
Wrapped Integer: 79
Primitive double: 3.14
Wrapped Double: 3.14
Primitive char: a
Wrapped Character: a

Example 2 :Wrapper Objects into Primitive Types

Run
public class Main {
    public static void main(String[] args) {
        // Example with Integer wrapper class
        Integer numObj = 50;
        int num = numObj.intValue();
        System.out.println("Integer object value: " + numObj);
        System.out.println("Primitive int value: " + num);

        // Example with Double wrapper class
        Double doubleObj = 3.14;
        double doubleValue = doubleObj.doubleValue();
        System.out.println("Double object value: " + doubleObj);
        System.out.println("Primitive double value: " + doubleValue);

        // Example with Boolean wrapper class
        Boolean boolObj = true;
        boolean boolValue = boolObj.booleanValue();
        System.out.println("Boolean object value: " + boolObj);
        System.out.println("Primitive boolean value: " + boolValue);

        // Example with Character wrapper class
        Character charObj = 'a';
        char charValue = charObj.charValue();
        System.out.println("Character object value: " + charObj);
        System.out.println("Primitive char value: " + charValue);
    }
}

Output

Integer object value: 50
Primitive int value: 50
Double object value: 3.14
Primitive double value: 3.14
Boolean object value: true
Primitive boolean value: true
Character object value: a
Primitive char value: a

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription