Compareto with Primitives -> Integer/Int

compareTo with primitives - Integer / int

For performance, it usually best to make the code as simple and clear as possible and this will often perform well (as the JIT will optimise this code best). In your case, the simplest examples are also likely to be the fastest.


I would do either

int cmp = a > b ? +1 : a < b ? -1 : 0;

or a longer version

int cmp;
if (a > b)
cmp = +1;
else if (a < b)
cmp = -1;
else
cmp = 0;

or

int cmp = Integer.compare(a, b); // in Java 7
int cmp = Double.compare(a, b); // before Java 7

It's best not to create an object if you don't need to.

Performance wise, the first is best.

If you know for sure that you won't get an overflow you can use

int cmp = a - b; // if you know there wont be an overflow.

you won't get faster than this.

Can I use the compareTo to compare two values of type int?

int is one of the few primitive types that are built into the Java language, and as you noticed, primitives cannot contain methods.

You can wrap them in the non-primitive Integer type, which is a class, and then do the comparison:

Integer.valueOf(a).compareTo(Integer.valueOf(b))

Better (because it doesn't create useless objects) is to use the static method that that class offers, which does take primitive ints as arguments:

Integer.compare(a, b)

comparison Integer object with primitive int

You can use Objects.equals:

if (a == null || ! Objects.equals(a, b)) { ... }

Compare Integer with primitive

See my results

    Integer a = 500;
long b = 500;
System.out.println(a == b);
System.out.println(a.equals(b));

output

true
false

this is because the first comparison uses unboxing

b == a.intValue()

which produces true, because in Java 500L == 500 is true.

The second comparison uses boxing

a.equals(Long.valueOf(b))

this produces false because a and b are instances of different classes. See Integer.equals impl:

public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}

Is primitive int also a Comparable?

Formally this is due to how your Java compiler is required to consider overload resolution. If an overload for your type is not found, then an overload that widens the type is considered. If no such overload exists, then one that would cause your primitive types to be auto-boxed to the corresponding boxed type is considered. A function specified with generics is an appropriate candidate.

In your case, the appropriate overload

public static <T extends Comparable<T>> T maximum(T x, T y, T z)

can be found, and compilation is successful.


A function is chosen with this precedence according to JLS 15.12.2:

  1. Type widening
  2. Auto-boxing
  3. Variable arguments

(Taken from my answer Why is f(Double x) a better match than f(double... x)?)

How can I compare primitive class types? Like Integer and Void?

Your code seems ok, but with a little problem:

Class type = m.getReturnType(); 
boolean result = type.equals(Integer.class);

result here will only evaluate to true if m's returning type is of the Integer class.

If it is an int that will evaluate to false.

To check if the return type is also a primitive type, you need to compare to Integer.TYPE (not .class) and similarly to the other types.


So change your code from:

if (type.equals(Integer.class)) {

to

if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {

and do the same for the other types. This will match methods like Integer getAge() and int getAge().



Related Topics



Leave a reply



Submit