Java Integer: what is faster comparison or subtraction?

Posted by Vladimir on Stack Overflow See other posts from Stack Overflow or by Vladimir
Published on 2010-04-28T10:59:44Z Indexed on 2010/04/29 0:57 UTC
Read the original article Hit count: 476

I've found that java.lang.Integer implementation of compareTo method looks as follows:

public int compareTo(Integer anotherInteger) {
int thisVal = this.value;
int anotherVal = anotherInteger.value;
return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}

The question is why use comparison instead of subtraction:

return thisVal - anotherVal;

© Stack Overflow or respective owner

Related posts about java

Related posts about integer