Java

자바 프로그래밍 (Java) - 33 : 레퍼런스 타입

n.han 2016. 7. 14. 11:40

- 레퍼런스 타입의 종류

 

 

자바 데이터 타입

프리미티브 타입

레퍼런스 타입

boolean

numeric

class

interface

enum

array

Class는 위와 같이 레퍼런스 타입이기 때문에, 클래스를 다른 메서드로 넘기는 것은 Call By Reference와 같다고 볼 수 있다.

 

따라서 그 메서드에서 인자로 넘겨지는 클래스의 내부 멤버를 변경하면 메서드가 종료되어 리턴되어도 그 값이 유지된다.

 

class Point {

    int x, y;                // 필드

    Point(int x, int y) {    // 생성자

        this.x = x;

        this.y = y;

    }

}

class RefTypeExample2 {

    public static void main(String args[]) {

        Point obj = new Point(10, 20);

        System.out.printf("(%d, %d) %n", obj.x, obj.y);

 

        rearrange(obj);

 

        System.out.printf("(%d, %d) %n", obj.x, obj.y);

    }

    static void rearrange(Point point) {

        point.x = 30;         

        point.y = 40;         

    }

}

출력 결과

(10, 20)

(30, 40)