참조 변수는 힙 메모리에 생성된 인스턴스를 가리킵니다. 그러면 참조 변수에 실제로 어떤 내용이 들어 있는지 출력해보겠습니다.
package classpart;
public class StudentTest2 {
public static void main(String[] args) {
Student student1 = new Student( );
student1.studentName = "안연수";
Student student2 = new Student( );
student2.studentName = "안승연";
System.out.println(student1);
System.out.println(student2); //참조변수 값 출력
}
}
힙 메모리에 생성된 인스턴스의 메모리 주소는 참조 변수에 저장됩니다.
출력 내용 : classpart.Student@54bedef2
classpart.Student@5caf905d
출력내용을 확인해 보면 '클래스이름@주소 값'입니다. 이 주소 값은 다른 말로 해시 코드값이라고도 합니다.
따라서 student1 변수를 사용하여 student1 인스턴스를 참조할 수 있습니다. 이때 student1을 참조 변수, 주소 값을 참조 값이라고 합니다.
'JAVA 프로그래밍 > Do it! 자바 프로그래밍 입문' 카테고리의 다른 글
constructor/PersonTest.java (0) | 2021.05.13 |
---|---|
constructor/Person.java (0) | 2021.05.13 |
인스턴스와 메모리 (0) | 2021.05.13 |
참조변수 사용법 (0) | 2021.05.13 |
classpart/StudentTest1.java (0) | 2021.05.13 |