Be sure to compare the result of Java compareTo with 0


A rudimentary little story about Java's compareTo. I want people who say, "The result of compareTo is that you don't know which one is right away. "

compareTo always compares to 0

The result of Comparable # compareTo should always be compared with 0. You may see code comparing to -1 or 1, which is incorrect.

The following is quoted from JavaDoc of standard API (I emphasize it) did)

Return value: ** Negative integer ** if this object is less than the specified object, ** Zero ** if equal, ** Positive integer ** if greater

As mentioned above, there is no guarantee that fixed values such as -1 and 1 will be returned. (Although it is often implemented to return -1 or 1)

Sample code

You can read and write obediently by calling compareTo on the left side of the comparison operator [^ 1] and writing 0 on the right side.


if (obj1.compareTo(obj2) == 0) {
  System.out.println("obj1 = obj2");

if (obj1.compareTo(obj2) != 0) {
  System.out.println("obj1 ≠ obj2");

if (obj1.compareTo(obj2) < 0) {
  System.out.println("obj1 < obj2");

if (obj1.compareTo(obj2) <= 0) {
  System.out.println("obj1 ≦ obj2");

if (obj1.compareTo(obj2) > 0) {
  System.out.println("obj1 > obj2");

if (obj1.compareTo(obj2) >= 0) {
  System.out.println("obj1 ≧ obj2");

at the end

If you make a thorough comparison with 0, you will not be confused by the result of compareTo.

[^ 1]: ==, <, etc.

Recommended Posts

Be sure to compare the result of Java compareTo with 0
Get the result of POST in Java
[Java] How to compare with equals method
Compare the elements of an array (Java)
Output of the book "Introduction to Java"
Let's express the result of analyzing Java bytecode with a class diagram
I tried to measure and compare the speed of GraalVM with JMH
Things to be aware of when writing Java
Calculate the similarity score of strings with JAVA
How to display the result of form input
[Java] Output the result of ffprobe -show_streams in JSON and map it to an object with Jackson
[Java] How to get the authority of the folder
Java Welcome to the Swamp of 2D Arrays
Java engineers now compare to learn the basic grammar of Ruby Part 1 (Basic, Variables)
I tried to compare the infrastructure technology of engineers these days with cooking.
Conditional branching of the result of SQL statement to search only one in Java
[Java] How to get the URL of the transition source
[Java] How to use compareTo method of Date class
CI the architecture of Java / Kotlin applications with ArchUnit
How to write Scala from the perspective of Java
[Java] How to get the maximum value of HashMap
Monitor the internal state of Java programs with Kubernetes
Check the behavior of Java Intrinsic Locks with bpftrace
HTTPS connection with Java to the self-signed certificate server
Check the result of generic parameter inference with JShell
Java: Use Stream to sort the contents of the collection
[Java] Things to be aware of when outputting FizzBuzz
I want to be aware of the contents of variables!
The story of making dto, dao-like with java, sqlite
Replace only part of the URL host with java
[Java] The problem that true was returned as a result of comparing Integer with ==
Summary of how to use the proxy set in IE when connecting with Java
[Java] Change the process according to the situation with the Strategy pattern
Only the top level Stream can be parallelized with Java Stream.
How to use trained model of tensorflow2.0 with Kotlin / Java
I tried to summarize the basics of kotlin and java
[Java] Coverage report could not be created with the combination of default method of Cobertura + interface
Get to the abbreviations from 5 examples of iterating Java lists
20190803_Java & k8s on Azure The story of going to the festival
Command to check the number and status of Java threads
[Java] Simplify the implementation of data history management with Reladomo
Initialization with an empty string to an instance of Java String type
[Java] To know the type information of type parameters at runtime
Connect to DB with Java
Connect to MySQL 8 with Java
Things to be aware of when writing code in Java
Memo to silently write the source that seems to be usable and the result of compilation execution-java
How to derive the last day of the month in Java
[Rails] Where to be careful in the description of validation
Compare Java 8 Optional with Swift
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
Whether to make the server side at the time of system rebuild with Kotlin or Java
Input to the Java console
I tried to express the result of before and after of Date class with a number line
Read the data of Shizuoka point cloud DB with Java and try to detect the tree height.
I tried to solve the problem of "multi-stage selection" with Ruby
[Java] Various summaries attached to the heads of classes and members
Returning to the beginning, getting started with Java ② Control statements, loop statements
[Java1.8 +] Get the date of the next x day of the week with LocalDate
I want to return to the previous screen with kotlin and java!
Java arguments to run with gradle run can be specified with --args (since Gradle 4.9)