Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[아이템 14] Comparable을 구현할지 고려하라 #13

Open
punsoo opened this issue Apr 19, 2021 · 2 comments
Open

[아이템 14] Comparable을 구현할지 고려하라 #13

punsoo opened this issue Apr 19, 2021 · 2 comments

Comments

@punsoo
Copy link
Member

punsoo commented Apr 19, 2021

p.91을 보면

compareTo 메서드에서 관계 연산자 <와 >를 사용하는 이전 방식은 거추장스럽고 오류를 유발하니, 이제는 추천하지 않는다.

라고 되어있습니다.

관계 연산자 <,> 가 어떤 오류를 유발하는 것이며 Integer.compare를 사용하였을 때 어떻게 방지된다는 것일까요?

@ghojeong
Copy link
Member

관계 연산자 <,> 가 어떤 오류를 유발하는 것이며 Integer.compare를 사용하였을 때 어떻게 방지된다는 것일까요?

흠... 저도 정확히 어떤 오류를 발생시키는 지는 모르겠네요.
하지만 한가지 말씀드릴 수 있는 것은 Java 가 연산자를 override 하는 것에 대해 매우 보수적인 언어라는 것입니다.
다른 분들께도 한번 도움을 요청해보겠습니다.

@punsoo
Copy link
Member Author

punsoo commented Apr 19, 2021

p.93을 보면

이 방식은 정수 오버플로를 이릉키거나 IEEE 754 부동소수점 계산 방식에 따른 오류를 낼 수 있다.
라고 되어있습니다.
정수 오버플로는 이해가는데 부동소수점은 어떤 연관이 있는건지 잘 모르겠군요...

그리고 p.93에 코드 14-4에

해시코드 값의 차를 기준으로 하는 비교자 - 추이성을 위배한다!

이거는 정수 오버플로나 부동소수점 오류 때문에 추이성을 위배한다는 뜻이겠지요???
compare 관련 의문점이 많아지는 날이네요

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants