coderz.py

Keep Coding Keep Cheering!

Autoboxing and Unboxing in Java

java thread class

Primitive data types are handled differently in Java, which led to the development of wrapper classes, which include the components Autoboxing and Unboxing.

Autoboxing:

Autoboxing is the process of transforming a primitive value into an object of the relevant wrapper class. Changing an int to the Integer class, for instance. When a primitive value is one of these, the Java compiler uses autoboxing:

  • passed as a parameter to a method that requests a wrapper class object as a parameter.
  • A variable of the relevant wrapper class was given the assignment.
Unboxing:

Unboxing describes the process of transforming a wrapper-type object to its corresponding primitive value. For instance, convert an int from an Integer. The Java compiler uses unbox when a wrapper class object is:

  • a value of the relevant primitive type is passed as a parameter to a method that anticipates it.
  • assigned to a primitive type variable of the appropriate kind.

NOTE: The benefit of autoboxing and unboxing is that less coding is needed because primitives and Wrappers don’t need to be manually converted.

Wrapper Classes in Java:

A class whose object contains or surrounds primitive data types is known as a wrapper class. Primitive data types can be stored in a field that is created when an object to a wrapper class is created. In other words, we can create a wrapper class object out of a primitive value.

PrimitiveWrapper class
intInteger
byteByte
shortShort
floatFloat
doubleDouble
charCharacter
longLong
booleanBoolean

The classes stated above fall under the numeric type wrapper. These classes contain the primitive types of byte, short, int, long, float, and double.

Example of Autoboxing:

Here, We have produced a list of integer-type entries. Instead of using integer objects, we are adding values of the primitive type int, and the code is correctly compiled. As the Java compiler converts the primitive int i into an Integer wrapped Object and adds it to the list, it does not cause a compile-time error.

// Importing required classes
import java.io.*;
import java.util.*;

// Main class
class Coderz {

	// Main driver method
	public static void main(String[] args)
	{

		// Creating an empty Arraylist of integer type
		ArrayList<Integer> ar = new ArrayList<Integer>();

		// Autoboxing
		ar.add(11);
		ar.add(901);
		ar.add(24);

		// Printing the ArrayList elements
		System.out.println("ArrayList: " + ar);
	}
}
Output:
ArrayList: [11, 901, 24]
Example of Unboxing:

import java.io.*;


class Coderz {

	// Main driver method
	public static void main(String[] args)
	{

		// Creating an Integer Object
		// with custom value say it be 20
		Integer i = new Integer(20);

		// Unboxing the Object
		int j = i;

		// Print statements
		System.out.println("Value of i:" + i);
		System.out.println("Value of j: " + j);

	}
}
Output:
Value of i:20
Value of j: 20
Java Program to Illustrate Find Sum of Even Numbers using Autoboxing and Unboxing:


import java.io.*;
import java.util.*;

// Main class
class Coderz {

	// Method 1
	// To sum even numbers
	public static int sum_Even(List<Integer> list)
	{

		// Initially setting sum to zero
		int sum = 0;

		for (Integer i : list) {

			// Unboxing of i automatically
			if (i % 2 == 0)
				sum += i;

			// Unboxing of i is done automatically
			// using intvalue implicitly
			if (i.intValue() % 2 == 0)
				sum += i.intValue();
		}

	
		return sum;
	}

	// Method 2
	// Main driver method
	public static void main(String[] args)
	{

		// Creating an empty ArrayList of integer type
		List<Integer> list = new ArrayList<Integer>();

		// Adding the int primitives type values to List
		for (int i = 0; i < 10; i++)
			list.add(i);

		// Getting sum of all even numbers in List
		int sum = sum_Even(list);

		System.out.println("Sum of even numbers = "
						+ sum);
	}
}
Output:
Sum of even numbers = 40

Note: also read about the Enums in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement