The Short.compareTo(Short anotherShort)
method in Java is used to compare two Short
objects numerically.
Table of Contents
- Introduction
compareTo(Short anotherShort)
Method Syntax- Examples
- Comparing Two Shorts
- Comparing Equal Shorts
- Comparing a Short with a Null Value
- Real-World Use Case
- Conclusion
Introduction
The Short.compareTo(Short anotherShort)
method is an instance method in the Short
class in Java. It compares two Short
objects numerically and returns an integer indicating their relative order. This method is useful when you need to sort or order Short
objects.
compareTo(Short anotherShort)() Method Syntax
The syntax for the Short.compareTo(Short anotherShort)
method is as follows:
public int compareTo(Short anotherShort)
- anotherShort: The
Short
object to be compared.
The method returns:
- A negative integer, zero, or a positive integer as this
Short
is less than, equal to, or greater than the specifiedShort
.
Examples
Comparing Two Shorts
The compareTo()
method can be used to compare two Short
objects numerically.
Example
public class CompareToExample {
public static void main(String[] args) {
Short short1 = 100;
Short short2 = 200;
int result = short1.compareTo(short2);
if (result < 0) {
System.out.println(short1 + " is less than " + short2);
} else if (result > 0) {
System.out.println(short1 + " is greater than " + short2);
} else {
System.out.println(short1 + " is equal to " + short2);
}
}
}
Output:
100 is less than 200
In this example, short1
(100) is less than short2
(200), so the method returns a negative integer.
Comparing Equal Shorts
The compareTo()
method returns zero when comparing two equal Short
objects.
Example
public class CompareEqualExample {
public static void main(String[] args) {
Short short1 = 150;
Short short2 = 150;
int result = short1.compareTo(short2);
if (result < 0) {
System.out.println(short1 + " is less than " + short2);
} else if (result > 0) {
System.out.println(short1 + " is greater than " + short2);
} else {
System.out.println(short1 + " is equal to " + short2);
}
}
}
Output:
150 is equal to 150
In this example, short1
(150) is equal to short2
(150), so the method returns zero.
Comparing a Short with a Null Value
When comparing a Short
object with null
, a NullPointerException
will be thrown. It’s important to handle null
values to avoid exceptions.
Example
public class CompareToNullExample {
public static void main(String[] args) {
Short short1 = 100;
Short short2 = null;
try {
int result = short1.compareTo(short2);
if (result < 0) {
System.out.println(short1 + " is less than " + short2);
} else if (result > 0) {
System.out.println(short1 + " is greater than " + short2);
} else {
System.out.println(short1 + " is equal to " + short2);
}
} catch (NullPointerException e) {
System.out.println("Cannot compare " + short1 + " with null.");
}
}
}
Output:
Cannot compare 100 with null.
In this example, comparing short1
(100) with short2
(null) throws a NullPointerException
, which is caught and handled.
Real-World Use Case
Sorting a List of Shorts
In a real-world application, you might use the compareTo()
method to sort a list of Short
objects.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortShortsExample {
public static void main(String[] args) {
List<Short> shortList = new ArrayList<>();
shortList.add((short) 50);
shortList.add((short) 20);
shortList.add((short) 30);
shortList.add((short) 10);
Collections.sort(shortList);
System.out.println("Sorted list: " + shortList);
}
}
Output:
Sorted list: [10, 20, 30, 50]
In this example, the Short.compareTo()
method is used to sort a list of Short
objects in ascending order.
Conclusion
The Short.compareTo(Short anotherShort)
method in Java is a powerful and useful tool for comparing Short
objects numerically. By understanding how to use this method, you can efficiently handle tasks that involve sorting or ordering Short
objects in your Java applications. Whether you are comparing positive or negative Short
values, or handling null
values, the compareTo()
method provides a reliable solution for these tasks.