Master java skills

Wrapper Class in Java

Below are given examples of wrapper classes in java with their corresponding Primitive data types:

Primitive TypeWrapper Class
byteByte
shortShort
intInteger
longLong
doubleDouble
booleanBoolean
charCharacter
floatFloat

Java provides a technique for converting a primitive into an object and an object into a primitive.
Autoboxing and unpacking features automatically turn primitives into objects and objects into primitives since J2SE 5.0. Autoboxing and unpacking are the automatic transformation of a primitive into an object.

In Java, a wrapper class is a class that wraps (or contains) a primitive data type into an object. The wrapper classes provide a way to use primitive data types as objects, which can be useful in situations where you need to pass a primitive data type to a method that requires an object.

Uses of Wrapper classes:

As Java is an object-oriented programming language, we frequently work with objects in Collections, Serialization, Synchronization, and other contexts. Let’s look at the various situations where using wrapper classes is necessary.

Change the value in Method: Java supports only call by value. Hence, if we pass a primitive value, the original value will not be changed. However, if we turn a primitive value into an object, the original value will be changed.

Serialization: To conduct the serialization, we must first convert the objects into streams. If we have a primitive value, we can use the wrapper classes to turn it into an object.

java.util package: It offers classes that are useful for working with objects.

Synchronization: It is applied to objects to ensure that multiple threads can access and modify shared resources in a mutually exclusive manner.

Collection Framework: The Java collection framework exclusively handles objects, as all of its classes (such as ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) are designed to operate solely on objects.

Autoboxing:

In Java, auto-boxing is a process where the Java compiler automatically converts primitive data types (such as int, double, etc.) to their corresponding object wrapper classes (such as Integer, Double, etc.) when needed. This conversion is done implicitly, without any explicit coding required from the programmer. Auto-boxing makes it easier to work with primitive data types as they can be used interchangeably with their object wrapper classes. However, it can also have an impact on performance as it involves creating new object instances.

int myInt = 42;// primitive int
Integer myInteger = myInt; // auto-boxing

In this code, we declare an int variable myInt and initialize it with the value 42. We then declare an Integer variable myInteger and assign it the value of myInt. This assignment triggers auto-boxing, which automatically creates a new Integer object with the value of myInt, and assigns it to myInteger.

Unboxing:

In Java, “unboxing” refers to the process of converting an object of a wrapper class (such as Integer, Double, etc.) to its corresponding primitive data type (such as int, double, etc.). This conversion is done implicitly, without any explicit coding required from the programmer.

Integer myInteger = 42;
int myInt = myInteger; // unboxing

In this code, we declare an Integer variable myInteger and initialize it with the value 42. We then declare an int variable myInt and assign it the value of myInteger. This assignment triggers unboxing, which automatically extracts the underlying primitive value from the Integer object, and assigns it to myInt.

Example:

public class WrapperExample {
    public static void main(String[] args) {
        int number = 10;
        Integer integer = Integer.valueOf(number); // autoboxing

        System.out.println("Number: " + number);
        System.out.println("Integer: " + integer);

        int sum = number + integer.intValue(); // unboxing
        System.out.println("Sum: " + sum);
    }
}
Output:

Number:10
Integer:10
Sum:20

In this example, we create an int variable called number with a value of 10. We then create an Integer object called integer by calling the valueOf() method of the Integer class and passing in the number variable. This is called “wrapping” the primitive int value in an object of the Integer class.

We then print out the values of number and integer using the println() method. We can see that integer is an object that contains the value of number.
Finally, we create a new int variable called sum by adding number and the result of calling the intValue() method on integer. This is called “unwrapping” the Integer object to get the primitive int value. We then print out the value of sum.

Implementation:

public class WrapperExample {
    public static void main(String[] args) {
        int num = 42;
        double numDouble = 3.14;
        char letter = 'A';

        Integer wrappedInt = Integer.valueOf(num);
        Double wrappedDouble = Double.valueOf(numDouble);
        Character wrappedChar = Character.valueOf(letter);

        System.out.println("Wrapped values:");
        System.out.println("Wrapped Integer: " + wrappedInt);
        System.out.println("Wrapped Double: " + wrappedDouble);
        System.out.println("Wrapped Character: " + wrappedChar);

        int unwrappedInt = wrappedInt.intValue();
        double unwrappedDouble = wrappedDouble.doubleValue();
        char unwrappedChar = wrappedChar.charValue();

        System.out.println("Unwrapped values:");
        System.out.println("Unwrapped Integer: " + unwrappedInt);
        System.out.println("Unwrapped Double: " + unwrappedDouble);
        System.out.println("Unwrapped Character: " + unwrappedChar);
    }
}
OUTPUT:

Wrapped values:
Wrapped Integer: 42
Wrapped Double: 3.14
Wrapped Character: A

Unwrapped values:
Unwrapped Integer: 42
Unwrapped Double: 3.14
Unwrapped Character: A

In this program, we first define three primitive variables: num of type int, numDouble of type double, and letter of type char.
We then create wrapper objects for each primitive variable using the valueOf method of the appropriate wrapper class (Integer, Double, and Character). The wrapped objects are assigned to the variables wrappedInt, wrappedDouble, and wrappedChar.
We print out the wrapped values using the System.out.println statement.
We then unwrap the values by calling the appropriate xxxValue() method on each wrapper object, where xxx is the name of the primitive type. We store the unwrapped values in unwrappedInt, unwrappedDouble, and unwrappedChar. Finally, we print out the unwrapped values using the System.out.println statement.