Java/Theory

[Java/Theory]17. AutoBoxing, Unboxing

양승길 2016. 5. 31. 12:07

17. AutoBoxing, Unboxing(JDK 1.5)

    Wraping의 번거로움을 해결한 과정.

    일일이 Implicit, Explicit Casting하였지만 직접 대입해도 무관

    Ex 1 : 

1
2
3
4
5
int intValue = 1;
ArrayList<Integer> arrayList = new ArrayList<Integer>();
 
arrayList.add(intValue);       //==>autoboxing
int i = arrayList.get(0);      //==>autounboxing
cs

    Ex 2 : 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void add15(int intValue,double doubleValue,boolean boo){
    //==> Autoboxing 기능 이해 ::  Method add14() 비교
    Integer i = intValue;        // 내부적수행 ==>Integer i = new Integer(intValue); 
    Double d = doubleValue;     // 내부적수행 ==>Double d = new Double(doubleValue); 
    Boolean b = boo;            // 내부적수행 ==>Boolean b = new Boolean(boo);
 
    //==>각각의 출력하면...
    System.out.println("i.toString() : "+i);
    System.out.println("i.intValue() : "+i.intValue());
    System.out.println("d.toString() : "+d);
    System.out.println("d.doubleValue() : "+d.doubleValue());
    System.out.println("b.toString () : "+ b);
    System.out.println("b.booleanValue() : "+ b.booleanValue());
    
    //==> Auto-unboxing 기능 이해.
    double result = i+d;          //내부적수행 ==>i.intValue()+d.doubleValue();
    System.out.println("합 : "+result);
}
cs