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

  • SEARCH
  • Home
  • 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 8322781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:29:55+00:00 2026-06-08T23:29:55+00:00

java.lang.Comparable#compareTo method states as first provision The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compare- To(x))

  • 0

java.lang.Comparable#compareTo method states as first provision

The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compare-
To(x)) for all x and y. (This implies that x.compareTo(y) must throw
an exception if and only if y.compareTo(x) throws an exception.)

and according Joshua Bloch in Effective Java in item 12

This trick works fine here but should be used with extreme caution.
Don’t use it unless you’re certain the fields in question are
non-negative or, more generally, that the difference between the
lowest and highest possible field values is less than or equal to
Integer.MAX_VALUE (231-1). The reason this trick doesn’t always work
is that a signed 32-bit integer isn’t big enough to hold the
difference between two arbitrary signed 32-bit integers. If i is a
large positive int and j is a large negative int, (i – j) will
overflow and return a negative value. The resulting compareTo method
will return incorrect results for some arguments and violate the first
and second provisions of the compareTo contract. This is not a purely
theoretical problem: it has caused failures in real systems. These
failures can be difficult to debug, as the broken compareTo method
works properly for most input values.

With integers overflow you can violate the first provision and I can’t find how, this example shows how the first provision would be violated:

public class ProblemsWithLargeIntegers implements Comparable<ProblemsWithLargeIntegers> {

    private int zas;

    @Override
    public int compareTo(ProblemsWithLargeIntegers o) {
        return zas - o.zas;
    }

    public ProblemsWithLargeIntegers(int zas) {
        this.zas = zas;
    }

    public static void main(String[] args) {
      int value1 = ...;
      int value2 = ...;
      ProblemsWithLargeIntegers d = new ProblemsWithLargeIntegers(value1);
      ProblemsWithLargeIntegers e = new ProblemsWithLargeIntegers(value2);
      if (!(Math.signum(d.compareTo(e)) == -Math.signum(e.compareTo(d)))){
        System.out.println("hey!");
    }

}

So I want a value1 and a value2 for getting that? Any idea? Or Joshua was wrong?

  • 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-06-08T23:29:58+00:00Added an answer on June 8, 2026 at 11:29 pm

    Well, this violates the general contract to start with. For example, take value1 = Integer.MIN_VALUE and value2 = 1. That will report that Integer.MIN_VALUE > 1, effectively.

    EDIT: Actually, I was wrong – it’s easy to violate the first provision:

    int value1 = Integer.MIN_VALUE;
    int value2 = 0;
    

    You’ll get a negative result for both comparisons, because Integer.MIN_VALUE - 0 == 0 - Integer.MIN_VALUE.

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

Sidebar

Related Questions

java.lang.IllegalStateException: Could not execute method of the activity . I am new to Android.
Exception in thread main java.lang.NullPointerException at Library.loadBooks(Library.java:179) at UseLibrary.main(UseLibrary.java:105) This error makes me crazy!
I am calling java.lang.Runtime.exec(...) but this seems to accept the command lines as an
In the following statement: java.lang.Object.wait(Object.java:???)[Native Method] - waiting on <0x6ab0eb48> (a coldfusion.runtime.RWLock) coldfusion.runtime.RWLock.waitForLock(RWLock.java:154) What
i got java.lang.NullPointerException by using CrudRepository my project looks like this: strut2 action service
java.lang.UnsupportedOperationException: This operation is not supported on Query Results at org.datanucleus.store.query.AbstractQueryResult.contains(AbstractQueryResult.java:250) at java.util.AbstractCollection.retainAll(AbstractCollection.java:369) at
java.lang.ClassNotFoundException: com.hazelcast.core.Hazelcast This bit is strange though, because i've added hazelcast-1.8.5.jar to the classpath
Consider this code: val foo = if(true) new java.lang.Double(4) else new java.lang.Integer(4) The inferred
If I implement java.lang.Comparable for a class, do I still have to override the
I'm using some java.util.Date (which implements java.lang.Comparable) and would like to be able to

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.