Java
[Java] hashCode(), toString()
dalooong
2023. 7. 9. 14:22
hashCode()
- 객체의 해시코드(hashcode)를 반환하는 메서드
- hashCode = 정수값
- Object 클래스의 hashCode()는 객체의 주소를 int로 변환해서 반환
public class Object {
public **native** int hashCode();
}
- equals()를 오버라이딩 하면, hashCode()도 오버라이딩 해야한다.
⇒ equals()의 결과가 true인 두 객체의 해시코드는 같아야하기 때문이다.
- System.*identityHashCode*(Object obj)는 Object 클래스의 haseCode()와 동일
toString(), toString()의 오버라이딩
- toString() : 객체를 문자열(String)으로 변환하기 위한 메서드
- getClass() : 설계도 / getName() : 클래스 이름 / @: at 위치 / toHexString : 16진수 / haseCode():객체주소
객체 == iv집합이므로 객체를 문자열로 반환한다는 것은 iv의 값을 문자열로 반환한다는 것과 같다.