참조형 매개변수
- 변수의 값을 읽고 변경할 수 있다. (read & write)
class Data {
int x;
}
class Ex6_7 {
public static void main(String[] args) {
Data d = new Data();
d.x = 10;
System.out.println("main() : x = " + d.x);
change(d);
System.out.println("----After change(d)----");
System.out.println("main() : x = " + d.x);
}
static void change(int x) {
d.x = 1000;
System.out.println("change() : x = " + d.x);
}
}
Data d = new Data();
d.x = 10;
change(d);
static void change(int x) {
d.x = 1000;
System.out.println("change() : x = " + d.x);
}


- change메소드가 호출되면서 참조변수 d의 값(주소)이 매개변수 d에 복사됨
이제 매개변수 d에 저장된 주소값을 x에 접근 가능.
- change메소드에서 매개변수 d로 x의 값을 1000으로 변경
System.out.println("----After change(d)----");
System.out.println("main() : x = " + d.x);

- change메소드가 종료되면 매개변수 x는 스택에서 제거됨
console
main() : x = 10
change() : x = 1000
----After change(d.x)----
main() : x = 1000
References
