• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • 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: 4623
50
  • 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: 4623
50
  • 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
 
Saloon Keeper
Posts: 4988
186
  • 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: 4623
50
  • 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: 5397
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: 41125
848
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: 4623
50
  • 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: 4623
50
  • 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
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic