Java/Java Concept

[Java] Wrapper 클래스

군우 2018. 3. 9. 16:22

래퍼클래스

기본 자료형의 데이터를 감싸는 래퍼클래스


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package classvar20;
 
public class AboutStaticWrapping {
    public static void main(String[] args)
    {
        Integer iValue1 = new Integer(10);
        Integer iValue2 = new Integer(10);
        
        if(iValue1 == iValue2)
            System.out.println("iValue1과 iValue2는 동일 인스턴스 참조");
        else
            System.out.println("iValue1과 iValue2는 다른 인스턴스 참조");
    
        Integer iValue3 = Integer.valueOf(10);
        Integer iValue4 = Integer.valueOf(10);
        
        if(iValue3 == iValue4)
            System.out.println("iValue3과 iValue4는 동일 인스턴스 참조");
        else
            System.out.println("iValue3과 iValue4는 다른 인스턴스 참조");
    }
}
//valueOf 메소드의 인스턴스 생성방식에 있다. Wrapper 클래스는 String 클래스와 
// 마찬가지로 인스턴스의 내용변경이 불가능 하다.  그래서 두개의 참조변수가
//하나의 인스턴스를 참조한다고 해서 문제가 되지않는다.
// valueOf 메소드는  인스턴스의 생성 요청으로 전달되는 값에 해당하는 인스턴스가 
//이미 만들어져진 상태이면, 새로운 인스턴스를 생성하지않고 기존 인스턴스의 참조값을 반환한다.
 
cs