Java: ==, .equals(), and compareTo()

  • Post author:
  • Post category:
  • Post comments:0 Comments
  • Post last modified:January 27, 2005
Comparison Primitives Objects
a == b, a != b Equal values Refer to the same object.
a.equals(b) N/A Compares values, if it’s defined for this class, as it is for most Java core classes. If it’s not defined for a (user) class, it behaves the same as ==.
a.compareTo(b) N/A Compares values. Class must implement the Comparable interface. All Java classes that have a natural ordering implement this (String, Double, BigInteger, …), but BigDecimal may not do what you expect. Comparable objects can be used by the Collections sort() method and data structures that implicitly sort (eg, TreeSet, TreeMap).
compareTo(a, b) N/A Compares values. Available only if the Comparator interface has been implemented, which is not the case for the Java classes. Typically used to define a comparator object that can be passed to Collections sort() method or ordered data structures.
Author Details
Kubernetes, DevSecOps, AWS, 클라우드 보안, 클라우드 비용관리, SaaS 의 활용과 내재화 등 소프트웨어 개발 전반에 도움이 필요하다면 도움을 요청하세요. 지인이라면 가볍게 도와드리겠습니다. 전문적인 도움이 필요하다면 저의 현업에 방해가 되지 않는 선에서 협의가능합니다.
0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments