• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

overriding compareTo()

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Core Java book volume one, there is this tip:

The compareTo method of the Comparable interface returns an integer. If the objects are not equal, it does not matter what negative or positive value you return. This flexibility can be useful when comparing integer fields. For example, suppose each employee has a unique integer id, and you want to sort by employee ID number. Then you can simply return id - other.id. That value will be some negative value if the first ID number is less than the other, 0 if they are the same ID, and some positive value otherwise. However, there is one caveat: The range of the integers must be small enough that the subtraction does not overflow. If you know that the IDs are not negative or that their absolute value is at most(Integer.MAX_VALUE - 1) / 2, you are safe.



i got everything in the tip except this part

that their absolute value is at most(Integer.MAX_VALUE - 1) / 2



What do they mean by that?

Thanks
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the reason:

If the condition mentioned does not hold, you can subtract smaller value from the larger one and still get negative number, due to arithmetic overflow.
 
Adam Johnson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Martin for your answer

But lets say we change the code to



so large becomes 2147483647 and small is -2147483647 obviously the result should be 0, but instead it yields -2

How is that possible?

I know that when we add 1 to Integer.MAX_VALUE we get -2147483648 hence it overflow the variable by 1.

but in the subtraction case above shouldn't it go back to 0 position?
 
Marshal
Posts: 80085
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

This is a standard problem with integer arithmetic called overflow; you can overflow positively or negatively. The largest number you can fit into two's complement arithmetic in 8 bits is 0111_1111 (decimal = +127), but if you add 1 you get 1000_0000, which in two's complement is -128. Similarly if you subtract 1 from 1000_0000 (-128) you get 0111_1111 (+127).
 
Sheriff
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Johnson wrote:so large becomes 2147483647 and small is -2147483647 obviously the result should be 0, but instead it yields -2


large + small == 0. large - small == 2147483647 - -2147483647 == 2147483647 + 2147483647 == 4294967294. Since ints can only range from -2147483648 to 2147483647 this number overflows to -2.

For comparing ints I usually use the following: In other words, -1 if value1 < value2, 1 if value1 > value2 and 0 if value1 == value2.
 
Adam Johnson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell and Rob thank you for your explanation

Rob Nice technique for handling comparison using the ternary operator

Adam
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic