🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
Shortobject to be compared.
The method returns:
- A negative integer, zero, or a positive integer as this
Shortis 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment