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 int
s 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:
- Type widening
- Auto-boxing
- 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
Are There Any Java Method Ordering Conventions
Httpget with Https:Sslpeerunverifiedexception
Print All the Spring Beans That Are Loaded
How to Get the Http Status Code Out of a Servletresponse in a Servletfilter
Error: Java_Home Is Not Defined Correctly Executing Maven
Java String.Replace/Replaceall Not Working
How to Capture Video Using Jmf, But Without Installing Jmf
Maven. Lambda Expressions Are Not Supported in -Source 1.5
What Is the Benefit of Polymorphism Using Collection Interface to Create Arraylist Object
Why Does This Gridbaglayout Not Appear as Planned
Factorial Using Recursion in Java
Export PDF Pages to a Series of Images in Java
How to Set Tls Version on Apache Httpclient
What Is Java_Home? How Does the Jvm Find the Javac Path Stored in Java_Home