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.
Answer from Peter Lawrey on Stack OverflowFor 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.
Use Integer.compare(int, int). And don'try to micro-optimize your code unless you can prove that you have a performance issue.
Is there any difference between Integer.compare(int a, int b) and a == b ?
How's the value of compareTo() result derived in java?
CompareTo error in Java, "error: int cannot be dereferenced"
[deleted by user]
Videos
I only found out about the .compare method when I let Intellij overwrite the equals method.
Can these be used interchageably?
The compareTo method returns three types of results:
A negative integer if the first string comes before the second string in lexicographical order.
Zero if the two strings are equal.
A positive integer if the first string comes after the second string in lexicographical order.
Note that it says positive/negative integer and not +1 or -1. How's the value of such integer derived?
I converted
String s1 = "Welcome to Java"; String s2 = "Programming is fun"; s1.compareTo(s2);
And I'm getting -7 as result from the java compiler. However, I don't understand how is this result coming?
I calculated the ASCII values of these strings.
s1=87 101 108 99 111 109 101 116 111 74 97 118 97
s2=80 114 111 103 114 97 109 105 110 103 105 115 102 117 110
It's not the net s1-s2 of ascii values, because that'd be a very large value.
Now, if I calculate +1 if s1>s2 and -1 if s1<s2, then I'd still not get -7.
Can anyone guide me?
References:
https://stackoverflow.com/questions/48041889/when-using-compareto-for-strings-returns-10-all-the-time
It seems like the returned value is implementation specific, thus I want to know say the oldest version of java's implementation.