Java
오토박싱, 오토언박싱
에어팟맥스
2022. 6. 9. 23:38
Boxing(박싱, 포장을 하는 것)이란?
==> 기본자료형(boolean, byte, short, int, long, char, float, double)으로 되어진 변수를
객체타입인 Wrapper 클래스(Boolean, Byte, Short, Integer, Long, Character, Float, Double)
타입의 객체로 만들어주는 것을 말한다.
Boxing(박싱) & Auto Boxing(오토 박싱) 예시
public class Main_Wrapper {
public static void main(String[] args) {
int a1 = 10;
Integer a2 = new Integer(a1); // Boxing(박싱)
System.out.println("a2 => " + a2); // 10
int b1 = 10;
Integer b2 = b1; // Auto Boxing(오토 박싱) // Integer 는 그대로 int 에 넣을 수 있음
System.out.println("b2 => " + b2); // 10
UnBoxing(언박싱, 포장을 푸는 것)이란?
==> Wrapper 클래스(Boolean, Byte, Short, Integer, Long, Character, Float, Double)로 되어진 객체를
기본자료형(boolean, byte, short, int, long, char, float, double)으로 만들어주는 것을 말한다.
UnBoxing(언박싱) & Auto UnBoxing(언박싱) 예시
Integer a3 = new Integer(20);
int a4 = a3.intValue(); // UnBoxing(언박싱)
System.out.println("a4 => " + a4); // a4 => 20
int a5 = new Integer(20); // Auto UnBoxing(언박싱) // int 는 그대로 Integer 에 넣을 수 있음
System.out.println("a5 => " + a5);
Double db1 = new Double(1.234567);
double db2 = db1.doubleValue();// UnBoxing(언박싱)
System.out.println("db2 => " + db2); // db2 => 1.234567
double db3 = new Double(1.234567); // Auto UnBoxing(언박싱)
System.out.println("db3 => " + db3); // db3 => 1.234567