Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 721081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:49:57+00:00 2026-05-14T05:49:57+00:00

When implementing compareTo() , does the degree of "difference" need to be taken into

  • 0

When implementing compareTo(), does the degree of "difference" need to be taken into account?

For instance, if I have 3 objects, C1, C2, and C3, such that C1 < C2 < C3.

Should C1.compareTo(C2) return an integer that is less than C2.compareTo(C3)?

The documentation for the Comparable interface doesn’t seem to specify one way or another, so I’m guessing the degree doesn’t matter, but it would be nice to know if there is some advantage returning a specific number (for example, improving TreeSet sort speed or something).

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html#compareTo(T)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:49:58+00:00Added an answer on May 14, 2026 at 5:49 am

    Interesting question, but nonetheless no, the magnitude of the int has no significance as per Comparable<T> and Comparator<T> specifications, only the sign. Conceivably some sorting algorithm can additionally specify that they can take “hints” from the magnitude, but I’m not sure how practical that would be for comparison-based sorting, since we really need only to know if a < b, a == b, or a > b (which is really what Comparable and Comparator are OOP abstractions of).


    Now it needs to be said that there may be a hidden intention here of using the subtraction idiom for comparing numeric values, i.e. something like this:

    public int compare(T t1, T t2) {
        return t1.intField - t2.intField;
    }
    

    Do note that this comparison method is potentially broken, due to possible overflow when the difference between the two numbers is greater than Integer.MAX_VALUE. In fact, this is one of the puzzles covered in Java Puzzlers.

    To demonstrate, consider the following snippet (taken from the book):

    int x = -2000000000;
    int z = 2000000000;
    System.out.println(x - z); // prints a positive number due to overflow
    

    Clearly x < z, and yet x - z is a positive number. Beware of using this subtraction idiom: it’s always much safer to do an explicit comparison and return -1, 0, or 1 instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.