staitc void copy(String src, String dst) throws IOException {
InputStream in = new FileInputStream(...);
try {
OutputStream out = new FileOutputStream(...);
try {
...
} finally {
out.close();
}
} finally {
in.close();
}
}
자원이 둘 이상 일 경우 finally를 두 번 사용해야한다.
static void copy(String src, String dst) throws IOException {
try (InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst)) {
byte[] buffer = new byte[BUFFER_SIZE];
int n;
while ((n = in.read(buffer) >= 0) {
out.write(buffer, 0, n);
}
}
}
Object 객체에서 재정의(overriding)를 염두에 두고 설계된 메서드다.
객체 식별성(object identity, 두 객체가 물리적(메모리주소)으로 같은가)이 아니라 논리적 동치성을 확인해야 하는데, 상위 클래스의 equals가 논리적 동치성을 확인하지 않는 경우
null이 아닌 모든 참조 값 x에 대해, x.equlas(x)는 true다.
null이 아닌 모든 참조 값 x, y에 대해, x.equals(y)가 true면 y.equals(x)도 true다.
null이 아닌 모든 참조 값 x, y, z에 대해, x.equals(y)가 true이고 y.equals(z)도 true면 x.equals(z)도 true다.
null이 아닌 모든 참조 값 x, y에 대해, x.equals(y)를 반복해서 호출하면 항상 true를 반환하거나 항상 false를 반환한다.
null이 아닌 모든 참조 값 x에 대해, x.equals(null)은 false다.