Short compareTo() method in Java with Examples
The compareTo() method of Short class is a built in method in Java which is used to compare twoShort objects numerically.
Syntax:
public int compareTo(Short otherShort)
Parameters : This method accepts a mandatory parameter otherShort which is the Short object to be compared.
Return type : It returns an int value. It returns:
- 0 if ‘otherShort’ is equal to ‘thisShort’,
- a positive value ‘thisShort’ is lesser than ‘otherShort’,
- a negative value ‘otherShort’ is greater than ‘thisShort’
Below is the implementation of compareTo() method in Java:
Example 1:
// Java code to demonstrate // Short compareTo() method class GFG { public static void main(String[] args) { // creating a Short object Short a = new Short("4"); // creating a Short object Short b = new Short("4"); // compareTo method in Short class int output = a.compareTo(b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } |
chevron_right
filter_none
Output:
Comparing 4 and 4 : 0
Example 2:
// Java code to demonstrate // Short compareTo() method class GFG { public static void main(String[] args) { // creating a Short object Short a = new Short("4"); // creating a Short object Short b = new Short("2"); // compareTo method in Short class int output = a.compareTo(b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } |
chevron_right
filter_none
Output:
Comparing 4 and 2 : 2
Example 3:
// Java code to demonstrate // Short compareTo() method class GFG { public static void main(String[] args) { // creating a Short object Short a = new Short("2"); // creating a Short object Short b = new Short("4"); // compareTo method in Short class int output = a.compareTo(b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } |
chevron_right
filter_none
Output:
Comparing 2 and 4 : -2
Recommended Posts:
- Java Guava | Shorts.indexOf(short[] array, short[] target) method with Examples
- Java Guava | Shorts.indexOf(short[] array, short target) method with Examples
- Short doubleValue() method in Java with Examples
- Short hashCode() method in Java with Examples
- Short equals() method in Java with Examples
- ShortBuffer put(int, short) method in Java With Examples
- Java.lang.Short toString() method in Java with Examples
- Java Guava | compare() method of Short Class with Examples
- ByteBuffer compareTo() method in Java With Examples
- Charset compareTo() method in Java with Examples
- Byte compareTo() method in Java with examples
- LocalDateTime compareTo() method in Java with Examples
- Instant compareTo() method in Java with Examples
- Boolean compareTo() method in Java with examples
- Calendar compareTo() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



