equals(Object obj)
public boolean equals(Object ojb) {
if (obj instance of Person)
return id == ((Person)obj).id;
else {
return false;
}
}
hashCode()
toString()
// Object 클래스에 정의 된 toString()
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
clone()
자신을 복제하여 새로운 인스턴스를 생성
인스턴스 변수의 값만 복사 → 참조 타입의 인스턴스 변수가 있는 클래스는 완전한 인스턴스 복제 X
복제할 클래스가 Clonable 인터페이스를 구현해야 함
clone()을 오버라이딩하면서 접근 제어자를 protected에서 public으로 변경해야 함
공변 반환타입
얕은 복사와 깊은 복사
class Circle implements Cloneable {
Point p; // 원점
double r; // 반지름
Circle(Point p, double r) {
this.p = p;
this.r = r;
}
// 얕은 복사 메서드
public Circle shallowCopy() {
Object obj = null;
try {
obj = super.clone();
} catch (CloneNotSupportedException e) {}
return (Circle) obj;
}
// 깊은 복사 메서드
public Circle deepCopy() {
Object obj = null;
try {
obj = super.clone();
} catch (CloneNotSupportedException e) {}
Circle c = (Circle) obj;
c.p = new Point(this.p.x, this.p.y);
return c;
}
getClass
변경 불가능한(immutable) 클래스
비교
문자열 리터럴
// "100"을 100으로 변환
int i1 = Integer.parseInt("100"); // 문자열 -> 기본형
int i2 = Integer.valueOf("100"); // 기본형 -> 문자열