728x90
공부하면서 이해가 안갔던 부분 중 하나...!이지만 다시 정리해보기로 했다!
둘의 기본 개념정의는..?
매개변수를 기본형으로 선언하면 단순히 저장된 값만 얻지만, 참조형으로 선언하면 값이 저장된 곳의 주소도
알 수 있기 때문에 값을 읽어 오는 것은 물론 값을 변경할 수도 있다.
아래의 두 예제를 주기적으로 봐야 할것같다..!
class Data { int x; } // Data 클래스 안에 선언된 int형 x 변수
class PrimitiveParaEx {
public static void main(String[] args) {
Data d = new Data(); // 참조변수 d를 이용해 Data객체 생성
d.x=10;
System.out.println("main() : x =" +d.x);
change(d.x);
System.out.println("After change(d.x)");
System.out.println("main() : x =" +d.x);
}
static void change(int x) { // 기본형 매개변수
x=1000;
System.out.println("change() : x = " +x);
}
}
class Data { int x; } // Data 클래스 안에 선언된 int형 x 변수
class ReferenceParaEx {
public static void main(String[] args) {
Data d = new Data(); // 참조변수 d를 이용해 Data객체 생성
d.x=10;
System.out.println("main() : x =" +d.x);
change(d);
System.out.println("After change(d.x)");
System.out.println("main() : x =" +d.x);
}
static void change(Data d) { // 참조형 매개변수
d.x=1000;
System.out.println("change() : x = " +d.x);
}
}
728x90
'Backend > JAVA' 카테고리의 다른 글
자바(8)- Stream(Input/Output) (2) | 2022.09.21 |
---|---|
자바(7)- Thread(1)(멀티 스레드/동기화/컨트롤) (1) | 2022.09.19 |
자바(6)-Class (변수와 메서드) (0) | 2022.09.12 |
자바(5)- 객체 배열 (2) | 2022.09.12 |
자바(4)- Class(instance의 생성과 사용) (0) | 2022.09.12 |