Introduction

Checking whether a year is a leap year is one of the most common beginner-level Java programs. At first glance, it appears to be a simple divisibility problem, but the complete rule contains an important exception that often surprises new programmers.

Many beginners assume that every year divisible by 4 is a leap year. While that's true for most years, century years (such as 1900 and 2100) follow a different rule. Understanding this exception is essential because it's a favorite interview question and demonstrates your understanding of conditional logic.

In this guide, you'll learn the complete Gregorian leap year rule, implement it using four different approaches in Java, understand why the rule exists, and explore common mistakes and best practices.

Advertisement

What Makes a Year a Leap Year?

According to the Gregorian calendar, a year is a leap year if:

  • It is divisible by 4, and

  • It is not divisible by 100,

OR

  • It is divisible by 400.

This can be summarized as:

A leap year is:

Divisible by 4
AND
Not divisible by 100

OR

Divisible by 400

Examples

Year Divisible by 4 Divisible by 100 Divisible by 400 Leap Year?
2024 ✅ Yes
1900 ❌ No
2000 ✅ Yes
2023 ❌ No

Method 1: Using Nested If-Else Statements

This approach follows the official leap year rule step by step, making it ideal for beginners.

Java Program

public class LeapYearCheck {

    public static void main(String[] args) {

        int year = 1900;

        if (year % 4 == 0) {

            if (year % 100 == 0) {

                if (year % 400 == 0) {
                    System.out.println(year + " is a leap year.");
                } else {
                    System.out.println(year + " is not a leap year.");
                }

            } else {
                System.out.println(year + " is a leap year.");
            }

        } else {
            System.out.println(year + " is not a leap year.");
        }
    }
}

Output

1900 is not a leap year.

Step-by-Step Execution (Year = 1900)

  1. Check:

year % 4 == 0

Result:

True
  1. Check:

year % 100 == 0

Result:

True
  1. Check:

year % 400 == 0

Result:

False

Therefore:

1900 is not a leap year.

Method 2: Using a Single Logical Condition

The same rule can be written as one boolean expression.

Java Program

public class LeapYearSingleCondition {

    public static void main(String[] args) {

        int year = 2000;

        boolean isLeap =
                (year % 4 == 0 && year % 100 != 0)
                || (year % 400 == 0);

        System.out.println(
                year +
                (isLeap ? " is a leap year." : " is not a leap year.")
        );
    }
}

Output

2000 is a leap year.

Understanding the Condition

(year % 4 == 0 && year % 100 != 0)

means:

  • divisible by 4

  • but not divisible by 100

The second part:

(year % 400 == 0)

handles century years such as:

  • 1600

  • 2000

  • 2400

The complete condition correctly implements the Gregorian calendar rule.


Method 3: Using the Ternary Operator

The ternary operator provides a compact one-line solution.

Java Program

public class LeapYearTernary {

    public static void main(String[] args) {

        int year = 2023;

        String result =
                ((year % 4 == 0 && year % 100 != 0)
                || (year % 400 == 0))
                ? "leap year"
                : "not a leap year";

        System.out.println(year + " is a " + result + ".");
    }
}

Output

2023 is not a leap year.

This approach is concise but may be slightly harder to read than using a boolean variable.


Method 4: Printing Leap Years in a Range

A common interview variation asks you to print all leap years between two given years.

Java Program

public class LeapYearsInRange {

    public static void main(String[] args) {

        int start = 1990;
        int end = 2025;

        System.out.println("Leap years between "
                + start + " and " + end + ":");

        for (int year = start; year <= end; year++) {

            boolean isLeap =
                    (year % 4 == 0 && year % 100 != 0)
                    || (year % 400 == 0);

            if (isLeap) {
                System.out.print(year + " ");
            }
        }
    }
}

Output

Leap years between 1990 and 2025:

1992 1996 2000 2004 2008 2012 2016 2020 2024

Why Does the Century-Year Exception Exist?

The leap year rule isn't arbitrary.

Earth takes approximately:

365.2425 days

to complete one orbit around the Sun.

If we simply added one extra day every four years, we'd assume a year lasts:

365.25 days

This small difference accumulates over centuries.

To keep calendars aligned with Earth's orbit:

  • century years are not leap years,

  • unless they are divisible by 400.

This correction keeps the Gregorian calendar remarkably accurate.


How Java Handles This Internally

The variable:

year

is a primitive int stored inside the method's stack frame.

Each expression:

year % 4
year % 100
year % 400

is evaluated using the CPU's arithmetic unit.

When using:

&&

and

||

Java performs short-circuit evaluation.

For example:

year % 4 == 0 && year % 100 != 0

If the first condition is false, Java skips evaluating the second one because the entire expression can never become true.

No heap memory or object creation occurs.


Real-Life Analogy

Imagine a clock that loses about one-quarter of a minute every day.

If you never corrected it, after four days it would be off by roughly one full minute.

So every four days you add one extra minute.

However, this correction slightly overcompensates over many years.

To fix that, you occasionally skip one of those extra minutes.

That's exactly how leap years work.

The century-year exception is the calendar's way of preventing long-term drift.


Comparison Table

Method Readability Best Used When
Nested If-Else Excellent Learning the rule step by step
Single Boolean Condition Very Good Production code
Ternary Operator Good Short utility programs
Range-Based Loop Very Good Printing multiple leap years

Best Practices

  • Prefer the single boolean expression for production code.

  • Always test your solution using:

    • 1900

    • 2000

    • 2024

  • Extract the logic into a reusable method like isLeapYear(int year).

  • When working with dates, consider using Java's java.time.Year class instead of implementing the logic manually.


Common Mistakes

Checking Only Divisibility by 4

Incorrect:

if (year % 4 == 0)

This incorrectly treats:

1900
2100
2200
2300

as leap years.


Incorrect Century Logic

Many beginners mistakenly assume:

Every century year is a leap year.

This is false.

Only century years divisible by 400 qualify.


Mixing Up && and ||

Using the wrong logical operator changes the entire meaning of the leap year rule.


Not Testing Edge Cases

Always verify your solution using:

  • 1900

  • 2000

  • 2024

  • 2100


Rewriting the Logic Everywhere

Instead of repeating the condition throughout your codebase, create a reusable helper method.


Expert Tips

  • Memorize the complete leap year rule instead of only "divisible by 4."

  • Mention 1900 and 2000 during interviews to demonstrate you understand the century-year exception.

  • Explain the historical reason behind the rule to show conceptual understanding.

  • Prefer a reusable boolean method for larger applications.


Pros and Cons

Method Advantages Disadvantages
Nested If-Else Easy to understand More verbose
Single Boolean Condition Concise and reusable Slightly harder for beginners
Ternary Operator Very compact Reduced readability for newcomers
Range Loop Easily prints multiple leap years Not intended for checking a single year

Frequently Asked Questions

What is the complete leap year rule?

A year is a leap year if it is divisible by 4 and not divisible by 100, unless it is also divisible by 400.


Why isn't 1900 a leap year?

Because it is divisible by 100 but not by 400.


Is the year 2000 a leap year?

Yes.

It is divisible by:

  • 4

  • 100

  • 400

so it satisfies the Gregorian leap year rule.


How can I check a leap year in one line?

Use:

(year % 4 == 0 && year % 100 != 0)
|| (year % 400 == 0)

Does Java provide a built-in leap year method?

Yes.

You can use:

java.time.Year.isLeap(year)

Why do leap years exist?

Because Earth's orbital period is approximately 365.2425 days, not exactly 365 days.


Can I print leap years in a range?

Yes.

Loop through each year and apply the leap year condition.


What is the time complexity of checking one year?

O(1)

Only a few modulus operations are performed.


Is 2100 a leap year?

No.

Although divisible by 4 and 100, it is not divisible by 400.


What happens if I check only divisibility by 4?

Your program will incorrectly classify years such as:

  • 1900

  • 2100

  • 2200

as leap years.


Is this a common interview question?

Yes.

It is frequently used to test conditional logic and attention to edge cases.


Does this rule apply to every calendar?

No.

The rule described here applies specifically to the Gregorian calendar.

Other calendar systems use different leap-year rules.