• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implement compareTo

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In OCP Oracle Certified Professional Java SE 8 Programmer II, on chapter 3, page 144 we have an implementation of compareTo method:

This implementation may give the wrong answer when there is overflow (comparing a large negative value to a large positive value).
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure it says java.util.Comparable?
 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexandru Dragoi wrote:In OCP Oracle Certified Professional Java SE 8 Programmer II, on chapter 3, page 144 we have an implementation of compareTo method:

This implementation may give the wrong answer when there is overflow (comparing a large negative value to a large positive value).


This is a limitation of compare and compareTo methods in that they return int instead of long. There is no way to code these methods to accommodate a range of ids that is wider than half the range of int.
But negative ids wouldn't make sense either
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I follow this.

If you have:

then you cover the whole range of the Int.
 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do that but ideally the value returned by the method should tell you about the distance between the two objects. If you just return -1 , 0, and 1, you won't be able to order the objects efficiently.
But you are right, "no way" is not the right phrase for what I had in mind.
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:If you just return -1 , 0, and 1, you won't be able to order the objects efficiently.



I really doubt that returned value decide the order of object.

Its just lazy n quick practice of returning a -b for comparison.

Or am I missing something ??
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
Are you sure? The spec doesn't say anything about the magnitude of the value mattering.

And I looked at the ComparableTimSort class in the JDK I am using. It doesn't use the magnitude; just the sign:

 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, Jeanne. As I wrote earlier, I had something else in mind. I was over optimizing. The magnitude of the difference can make ordering a lot more efficient but it is not a goal of compare method.
OP's claim about the code in your book is correct. It will not work for extreme cases.
 
Alexandru Dragoi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:OP's claim about the code in your book is correct. It will not work for extreme cases.



If you just set up some invariants for that object state everything would be fine. As you said, negative ids do not make sense.
 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, every piece of code will work, once all errors are fixed
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic