Dale King wrote:
> - wrote:
>
>>Christian Kaufhold wrote:
>>
>>
>>>No, to return 0 then, as the state in question is equal.
>>
>>
>>Oh i get it
>>
>>int value = 0;
>>
>>if (a1.getX() < a2.getX()) {
>> value = -1;
>>} else if (a1.getX() > a2.getX()) {
>> value = 1;
>>}
>>
>>return 0;
>
>
> Note that Comparator does not require that the values be restricted to
> -1, 0, and 1. You can use any negative number instead of -1 and any
> positive number instead of 1. Therefore if getX returns an integer the
> above can be replaced with:
>
> return a1.getX() - a2.getX();
Not always. For example, consider what happens if
a1.getX() returns zero and a2.getX() returns INT_MIN.
This substitution is appropriate only if you're 100%
sure the subtraction cannot produce "wraparound."
--