Primitive data types are handled differently in Java, which led to the development of wrapper classes, which include the components Autoboxing and Unboxing.
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:
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:
NOTE: The benefit of autoboxing and unboxing is that less coding is needed because primitives and Wrappers don’t need to be manually converted.
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.
Primitive | Wrapper class |
---|---|
int | Integer |
byte | Byte |
short | Short |
float | Float |
double | Double |
char | Character |
long | Long |
boolean | Boolean |
The classes stated above fall under the numeric type wrapper. These classes contain the primitive types of byte, short, int, long, float, and double.
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);
}
}
ArrayList: [11, 901, 24]
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);
}
}
Value of i:20
Value of j: 20
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);
}
}
Sum of even numbers = 40
Note: also read about the Enums in Java
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…