Auto-boxing and (auto)Unboxing
Auto-boxing : primitive type=>object Unboxing : object=>primitive type
ArrayList<Integer> list = new ArrayList<Integer>();
//only object is possible but compiler does automatically
list.add(10);//list.add(new Integer(10));
int value = list.get(0);//list.get(0).intValue()
intValue(), valueOf() : Integer -> int
int i = 10;
//기본형을 참조형으로 형변환(형변환 생략가능)
Integer intg = (Integer)i;//Integer intg = Integer.valueOf(i);
Object obj = (Object)i;//Object obj = (Object)Integer.valueOf(i);
//참조형 변수
Long lng = 100L;//Long lng = new Long(100L);
//참조형과 기본형간의 연산 가능
int i2 = intg + 10;
//참조형간의 덧셈도 가능
long l = intg + lng;
Integer intg2 = new Integer(20);
int i3 = (int)intg2;//참조형을 기본형으로 형변환도 가능(형변환 생략가능)
Last updated
Was this helpful?