Java Integer compareTo() – why use comparison vs. subtraction?

This is due to integer overflow. When thisVal is very large and anotherVal is negative then subtracting the latter from the former yields a result that is bigger than thisVal which may overflow to the negative range.

Leave a Comment