public class Ex06 {
public static void main(String[] args) {
Test6 ob1; // 레퍼런스 변수
// ob1.print(); 컴오류. 메모리할당이 안되었으므로
ob1 = new Test6(); // 메모리 할당이 되고 a=10, b=20으로 초기화
// 필드 a=10, b=20을 갖고 있는 주소값을 가지고 있다.
Test6 ob2 = new Test6(); // 필드 a,b는 메모리 할당이 되고 a=10, b=20으로 초기화
System.out.println(ob1 == ob2); // 주소가 같냐? false
ob1.a+=100;
System.out.println(ob1.a + ":"+ ob2.a); // 110 : 10
Test6 ob3 = ob2; // ob와 ob3은 같은 주소를 나타낸다. 같은 객체
System.out.println(ob2 == ob3); // 주소가 같냐? true
ob3.a +=200;
System.out.println(ob2.a+":"+ob3.a); // 210 : 210
// 클래스명@해쉬코드 : 자바가 객체를 찾을 때 쓰는 코드
// 같은 객체는 해쉬코드가 같지만, 해쉬코드가 같다고 같은 객체는 아니다.★
System.out.println(ob1); // ex0714.Test6@15db9742
System.out.println(ob2); // ex0714.Test6@6d06d69c
System.out.println(ob3); // ex0714.Test6@6d06d69c
}
}
class Test6 {
int a=10;
int b=20;
public void print() {
System.out.println(a+":"+b);
}
}