예제 1
(AutoboxingEx1.java)
//랩퍼클래스
class AutoboxingEx1 {
Integer var;
public void setInt(int i){
var = new Integer(i);
}
public Integer getInt(){
return var;
}
public static void main(String[] args){
AutoboxingEx1 a1 = new AutoboxingEx1();
a1.setInt(10000);
Integer temp = a1.getInt();
int res = temp.intValue();
System.out.println("res :"+res);
}
}
예제 2
(AutoboxingEx2.java)
class AutoboxingEx2 {
Integer var;
public void setInt(int i){
var = i; //오토박싱
}
public Integer getInt(){
return var;
}
public static void main(String[] args){
AutoboxingEx1 a1 = new AutoboxingEx1();
a1.setInt(10000);
int res = a1.getInt(); //언박싱
System.out.println("res :"+res);
}
}//랩퍼클래스와 기본데이터타입간에만 성립....
예제 3
(AutoboxingEx3.java)
class AutoboxingEx3 {
public static void main(String[] args){
Integer[] var = {100,200,300};
for(int i : var) //언박싱
System.out.println(i);
}
}