<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/what-is-the-difference-between-equals-and-compareto-javalanguage-javacoding-javatips-jacxi3ayf" -->

---
title: What is the difference between equals() and compareTo()?...
description: Explains the difference between Java's equals() method and compareTo(): equals() returns a boolean and comes from the Object class, while compareTo() returns...
canonical: https://daily.dev/posts/what-is-the-difference-between-equals-and-compareto-javalanguage-javacoding-javatips-jacxi3ayf
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: What is the difference between equals() and compareTo()? #javalanguage #javacoding #javatips | daily.dev
og:description: Explains the difference between Java's equals() method and compareTo(): equals() returns a boolean and comes from the Object class, while compareTo() returns...
og:url: https://daily.dev/posts/what-is-the-difference-between-equals-and-compareto-javalanguage-javacoding-javatips-jacxi3ayf
og:image: https://api.daily.dev/og/posts/jaCXI3aYf.png
og:image:alt: What is the difference between equals() and compareTo()? #javalanguage #javacoding #javatips
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# What is the difference between equals() and compareTo()? #javalanguage #javacoding #javatips

**[Java \(Official Oracle\)](https://daily.dev/sources/java-oracle)** · 1 min read · 1 upvotes · 0 comments

## Summary

Explains the difference between Java's equals() method and compareTo(): equals() returns a boolean and comes from the Object class, while compareTo() returns an int and is defined on the Comparable interface implemented by String, wrapper types, and custom classes. Covers the consistency contract between the two (equal objects must return zero when compared) and warns that violating it causes subtle runtime bugs. Notes that BigDecimal has surprising equality rules, where 1.0 is not considered equal to 1.00.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=bsB3rAGONI8>

## Questions this post answers

### What is the difference between equals() and compareTo() in Java?

equals() returns a boolean and is defined on the Object class, callable on any object, while compareTo() returns an int and is defined on the Comparable interface, implemented by String, wrapper types like Integer and Double, and custom classes. equals() checks if two objects are equal, whereas compareTo() returns negative, positive, or zero depending on whether the object is less than, greater than, or equal to another.

_Developers studying Java core method contracts can keep sharpening that knowledge on daily.dev._

### Why should compareTo() be consistent with equals() in Java?

Two objects considered equal by equals() should return zero when compared with compareTo(), and two different objects should not return zero. Breaking this consistency contract between equals() and compareTo() can introduce subtle, hard-to-trace bugs at runtime, especially in sorted collections or data structures that rely on comparison logic.

_Those debugging sorting and collection bugs in Java often compare notes on daily.dev._

### Why does BigDecimal say 1.0 is not equal to 1.00 in Java?

BigDecimal's equals() method considers scale as part of equality, so 1.0 and 1.00 are treated as different values even though they represent the same number mathematically. This surprises developers who expect numeric equality; using compareTo() instead of equals() avoids this scale-sensitive pitfall when checking numeric equivalence.

_Java developers handling precise decimal math often trade these gotchas on daily.dev._

## Similar posts on daily.dev

- [The Object Class in Java](https://daily.dev/posts/the-object-class-in-java-epliabj8s) · Baeldung · 3 upvotes · 0 comments

---

Tags: [#java](https://daily.dev/tags/java)

[View this post on daily.dev](https://daily.dev/posts/what-is-the-difference-between-equals-and-compareto-javalanguage-javacoding-javatips-jacxi3ayf)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"What is the difference between equals() and compareTo()? #javalanguage #javacoding #javatips","url":"https://daily.dev/posts/what-is-the-difference-between-equals-and-compareto-javalanguage-javacoding-javatips-jacxi3ayf","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/what-is-the-difference-between-equals-and-compareto-javalanguage-javacoding-javatips-jacxi3ayf"},"datePublished":"2026-10-09T10:06:15.578Z","dateModified":"2026-10-09T10:06:32.838Z","description":"Explains the difference between Java's equals() method and compareTo(): equals() returns a boolean and comes from the Object class, while compareTo() returns...","image":"https://i.ytimg.com/vi/bsB3rAGONI8/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/bsB3rAGONI8/sddefault.jpg","isAccessibleForFree":true,"articleSection":"Java (Official Oracle)","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Java (Official Oracle)","logo":"https://media.daily.dev/image/upload/s--SM9hIYUY--/f_auto,q_auto/v1773648791/logos/java-oracle?_a=BAMAMiiu0","url":"https://daily.dev/sources/java-oracle"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/what-is-the-difference-between-equals-and-compareto-javalanguage-javacoding-javatips-jacxi3ayf","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java","timeRequired":"PT1M","video":{"@type":"VideoObject","name":"What is the difference between equals() and compareTo()? #javalanguage #javacoding #javatips","description":"Explains the difference between Java's equals() method and compareTo(): equals() returns a boolean and comes from the Object class, while compareTo() returns...","thumbnailUrl":"https://i.ytimg.com/vi/bsB3rAGONI8/sddefault.jpg","uploadDate":"2026-10-09T10:06:15.578Z","duration":"PT1M","url":"https://api.daily.dev/r/jaCXI3aYf","embedUrl":"https://www.youtube.com/embed/bsB3rAGONI8"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Java (Official Oracle)","item":"https://daily.dev/sources/java-oracle"},{"@type":"ListItem","position":3,"name":"What is the difference between equals() and compareTo()? #javalanguage #javacoding #javatips"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/what-is-the-difference-between-equals-and-compareto-javalanguage-javacoding-javatips-jacxi3ayf#faq","mainEntity":[{"@type":"Question","name":"What is the difference between equals() and compareTo() in Java?","acceptedAnswer":{"@type":"Answer","text":"equals() returns a boolean and is defined on the Object class, callable on any object, while compareTo() returns an int and is defined on the Comparable interface, implemented by String, wrapper types like Integer and Double, and custom classes. equals() checks if two objects are equal, whereas compareTo() returns negative, positive, or zero depending on whether the object is less than, greater than, or equal to another. Developers studying Java core method contracts can keep sharpening that knowledge on daily.dev."}},{"@type":"Question","name":"Why should compareTo() be consistent with equals() in Java?","acceptedAnswer":{"@type":"Answer","text":"Two objects considered equal by equals() should return zero when compared with compareTo(), and two different objects should not return zero. Breaking this consistency contract between equals() and compareTo() can introduce subtle, hard-to-trace bugs at runtime, especially in sorted collections or data structures that rely on comparison logic. Those debugging sorting and collection bugs in Java often compare notes on daily.dev."}},{"@type":"Question","name":"Why does BigDecimal say 1.0 is not equal to 1.00 in Java?","acceptedAnswer":{"@type":"Answer","text":"BigDecimal's equals() method considers scale as part of equality, so 1.0 and 1.00 are treated as different values even though they represent the same number mathematically. This surprises developers who expect numeric equality; using compareTo() instead of equals() avoids this scale-sensitive pitfall when checking numeric equivalence. Java developers handling precise decimal math often trade these gotchas on daily.dev."}}]}
```

