0

It is well known that since JDK7, java has regulated custom comparators, however, how does java verify that this construct is violated when the runtime?

  • 1
    Specifically this answer: https://stackoverflow.com/a/20236925/29470 The verification is done by the sorting algorithm. – Tim Moore Jan 10 '22 at 01:41

1 Answers1

1

It would not be possible to write a general program that verifies the correct behaviour of comparators in all cases due to the Halting problem. Therefore, Java does not verify your comparator for you.

However, algorithms that get your comparator as input might (accidentally) find out during runtime that your comparator does something that should be impossible, so they then inform you that the mistake is in your comparator and you should update your comparator to comply with the rules. Keep in mind, however, that they might not come across the error. They are not meant to check your comparator for you. In general, you are responsible for the correctness of your code.

An example of the error being thrown and how to fix it can be found here.

Max Meijer
  • 1,530
  • 1
  • 14
  • 23