-
Boxing, unboxing, and autoboxingStaticPL/JAVA 2019. 9. 27. 11:20
1. Overview
Let's clarify what boxing, unboxing, and autoboxing are and Why it needs and how to work.
2. Description
2.1 Definition
Name Definition Invoking cases Autoboxing Converting a primitive value into an object of the corresponding wrapper class - Passed as a parameter to a method that expects an object of the corresponding wrapper class
- Assigned to a variable of the corresponding wrapper class
(auto)Unboxing Converting an object of a wrapper type to its corresponding primitive value - Passed as a parameter to a method that expects a value of the corresponding primitive type
- Assigned to a variable of the corresponding primitive type
2.2 Corresponding Table of each type
Primitive type Wrapper Class boolean Boolean byte Byte char Character float Float int Integer long Long short Short double Double 3. Examples
3.1 Common cases
// Java program to illustrate the concept // of Autoboxing and Unboxing import java.io.*; class GFG { public static void main (String[] args) { // creating an Integer Object // with value 10. // also can treat it boxing(explicitly) Integer i = new Integer(10); // (auto)unboxing the Object int i1 = i; System.out.println("Value of i: " + i); System.out.println("Value of i1: " + i1); //Autoboxing of char Character gfg = 'a'; // Auto-unboxing of Character char ch = gfg; System.out.println("Value of ch: " + ch); System.out.println("Value of gfg: " + gfg); } }
3.2 Generics related cases
/* Java program to illustrate autoboxing */ import java.io.*; import java.util.*; class GFG { public static void main (String[] args) { /* Here we are creating a list of elements of Integer type. adding the int primitives type values */ List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < 10; i++) list.add(i); } } /* Java program to illustrate autoboxing */ import java.io.*; import java.util.*; class GFG { public static void main (String[] args) { /* Here we are creating a list of elements of Integer type. Adding the int primitives type values by converting them into Integer wrapper Object*/ List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < 10; i++) // here is the autoboxing occurred list.add(Integer.valueOf(i)); } } // Java program to illustrate find sum // of odd numbers using autobxing and unboxing import java.io.*; import java.util.*; class GFG { public static int sumOfOddNumber(List<Integer> list) { 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; } public static void main (String[] args) { /* Here we are creating a list of elements of Integer type and adding the int primitives type values to the list*/ List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < 10; i++) list.add(i); // getting sum of all odd no. in the list. int sumOdd = sumOfOddNumber(list); System.out.println("Sum of odd numbers = " + sumOdd); } }
4. References
https://www.geeksforgeeks.org/autoboxing-unboxing-java/
https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
https://www.studytonight.com/java/autoboxing-unboxing-java.php
'StaticPL > JAVA' 카테고리의 다른 글
Access Modifier (0) 2020.02.23 Is-A and Has-A Relationship in Java (0) 2020.02.05 Functional (0) 2019.08.27 Generics and Generic Methods (0) 2019.08.27 Reflection (0) 2019.08.27