• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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: 4650
51
  • 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: 4650
51
  • 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: 5157
207
  • 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: 4650
51
  • 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: 41405
853
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: 4650
51
  • 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: 4650
51
  • 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 kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic