There’s no error with the former because it’s using the compareTo() method which has overloads for all the primitive numeric types.
However, the latter is using the equals() method which - to cut a long story short - gives an error unless the types of the expressions on either side of the equals sign are the same.
This is an unsatisfactory situation and we had a long discussion about it recently in this thread.