Compare short values
In this chapter you will learn:
Compare two short values
We can use the compareTo(Short anotherShort
) and equals(Object obj)
from Short class to
do the comparison.
int compareTo(Short anotherShort)
compares two Short objects numerically.boolean equals(Object obj)
compares this object to the specified object.
compareTo(Short anotherShort)
compares two Short objects numerically.
It returns
Value Returned | Meaning |
---|---|
0 | if this Short is equal to the argument Short; |
< 0 | if this Short is numerically less than the argument Short; and |
> 0 | if this Short is numerically greater than the argument Short. |
public class Main {
public static void main(String[] args) {
Short shortValue1 = new Short("10");
Short shortValue2 = new Short("11");
// j av a 2 s .c om
System.out.println(shortValue1.compareTo(shortValue2));
}
}
The output:
If you just want to check the equality for two short values, equals
method from Short class
is the way to go.
public class Main {
public static void main(String[] args) {
Short shortValue1 = new Short("10");
Short shortValue2 = new Short("11");
//ja va 2s .com
System.out.println(shortValue1.equals(shortValue2));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Primitive Data Types