The short data type is a 16-bit signed two’s complement integer that provides a range of values from -32,768 (-2¹⁵) to 32,767 (2¹⁵ - 1). Similar to the byte data type, the short data type is also useful for saving memory because it takes up less space compared to an int, occupying only half its size. The default value for a short variable is 0.
Syntax:
Source Code:
public class ShortYearExample {
public static void main(String[] args) {
// Define short values for years
short startYear = 1990;
short endYear = 2025;
// Calculate the difference between the two years
short yearDifference = (short) Math.abs(endYear - startYear);
// Print year difference
System.out.println("Start Year: " + startYear);
System.out.println("End Year: " + endYear);
System.out.println("Difference between the years: " + yearDifference);
// Leap year check for predefined years
short[] years = { 2000, 2024, 2026, 2028, 2030};
System.out.println("\nLeap Year Checks:");
for (short year : years) {
if (isLeapYear(year)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
// Method to check if a year is a leap year
public static boolean isLeapYear(short year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}Result:
Watch the video:
Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

