Introduction

Finding the largest among three numbers is one of the first comparison-based programming exercises you'll encounter in Java. Unlike checking whether a number is even or positive, this problem requires comparing multiple values against each other to determine which one is the greatest.

Although the program appears simple, it introduces important concepts such as relational operators, logical operators, nested conditions, and built-in utility methods. The same comparison logic is used in many real-world scenarios, including finding the maximum element in an array, sorting algorithms, and data processing applications.

In this tutorial, you'll learn four different ways to find the largest of three numbers in Java, understand how each approach works internally, compare their advantages and disadvantages, and explore common interview questions related to this topic.

Advertisement

Understanding the Problem

Suppose you have three numbers:

a = 25
b = 78
c = 87

The goal is to determine which number is the largest.

Unlike comparing two numbers, where only one comparison is needed, comparing three numbers requires ensuring that the selected number is greater than or equal to both of the remaining numbers.


Method 1: Using If-Else with Logical AND

This is the standard and most commonly used approach.

Java Program

public class Main {

    public static void main(String[] args) {

        int a = 25;
        int b = 78;
        int c = 87;

        if (a >= b && a >= c) {
            System.out.println(a + " is the largest number.");
        } else if (b >= a && b >= c) {
            System.out.println(b + " is the largest number.");
        } else {
            System.out.println(c + " is the largest number.");
        }
    }
}

Output

87 is the largest number.

The logical AND (&&) operator ensures that a number is greater than or equal to both of the other numbers before it is declared the largest.

Time Complexity

O(1)

Space Complexity

O(1)


Method 2: Using Nested If-Else Statements

Another approach compares two numbers first and then compares the winner with the remaining number.

Java Program

public class Main {

    public static void main(String[] args) {

        int a = 25;
        int b = 78;
        int c = 87;
        int largest;

        if (a > b) {
            if (a > c) {
                largest = a;
            } else {
                largest = c;
            }
        } else {
            if (b > c) {
                largest = b;
            } else {
                largest = c;
            }
        }

        System.out.println("The largest number is: " + largest);
    }
}

Output

The largest number is: 87

This approach works like an elimination process, comparing two numbers first and then comparing the larger one with the remaining number.

Time Complexity

O(1)

Space Complexity

O(1)


Method 3: Using Math.max()

Java provides the built-in Math.max() method to compare two numbers.

For three numbers, simply nest two Math.max() calls.

Java Program

public class Main {

    public static void main(String[] args) {

        int a = 25;
        int b = 78;
        int c = 87;

        int largest = Math.max(a, Math.max(b, c));

        System.out.println("The largest number is: " + largest);
    }
}

Output

The largest number is: 87

The inner Math.max(b, c) finds the larger of b and c, and the outer Math.max() compares that result with a.

Time Complexity

O(1)

Space Complexity

O(1)


Method 4: Using the Ternary Operator

A nested ternary operator provides a compact one-line solution.

Java Program

public class Main {

    public static void main(String[] args) {

        int a = 25;
        int b = 78;
        int c = 87;

        int largest = (a >= b && a >= c)
                ? a
                : (b >= a && b >= c)
                    ? b
                    : c;

        System.out.println("The largest number is: " + largest);
    }
}

Output

The largest number is: 87

This approach is concise but becomes difficult to read if more conditions are added.

Time Complexity

O(1)

Space Complexity

O(1)


How Java Handles This Internally

Consider the variables:

int a = 25;
int b = 78;
int c = 87;

Internally:

  1. a, b, and c are primitive int variables stored in the stack memory.

  2. Java evaluates relational operators (>, >=) one by one.

  3. The logical AND (&&) operator uses short-circuit evaluation. If the first comparison is false, Java skips evaluating the second comparison because the overall result can never become true.

  4. Math.max() internally performs a simple comparison and returns the larger primitive value.

  5. After the main() method completes, all local variables are automatically removed from the stack.


Real-Life Analogy

Imagine three friends standing together, and you want to determine who is the tallest.

You compare the first two people and remember the taller one. Then you compare that person with the third friend.

Whoever remains after both comparisons is the tallest.

Finding the largest number works exactly the same way.


Comparison of Different Methods

Method Readability Best Used When
If-Else with Logical AND Excellent General-purpose programming
Nested If-Else Good Learning comparison logic
Math.max() Excellent Production code
Ternary Operator Good for small programs Compact one-line expressions

Best Practices

  • Prefer Math.max() in production code because it is concise and easy to read.

  • Use >= instead of > when equal values should be handled correctly.

  • For four or more numbers, store values in an array and use a loop or Java Streams.

  • Avoid deeply nested ternary expressions because they reduce readability.

  • Use meaningful variable names such as largest or maxValue.


Common Mistakes

Forgetting One Comparison

Incorrect:

if (a > b)

This only checks whether a is greater than b.

It completely ignores c, which may actually be the largest number.


Using OR Instead of AND

Incorrect:

if (a > b || a > c)

A number is considered the largest only if it is greater than both other numbers.

Always use &&, not ||.


Using Strict Greater Than

Using > instead of >= can produce incorrect results when two numbers are equal.


Assuming Math.max() Accepts Three Arguments

Incorrect:

Math.max(a, b, c)

Math.max() accepts only two arguments.

Use nested calls:

Math.max(a, Math.max(b, c))

Using Nested Ternary Operators Excessively

Deeply nested ternary expressions become difficult to read and maintain.


Expert Tips

  • Start with the if-else approach during interviews because it clearly demonstrates your understanding of logical operators.

  • Mention Math.max() as a cleaner alternative.

  • Explain that Math.max() can be nested for additional numbers.

  • For larger collections of numbers, recommend using arrays with loops or Java Streams.


Pros and Cons

Method Advantages Disadvantages
If-Else with Logical AND Easy to understand Becomes lengthy for many numbers
Nested If-Else Demonstrates comparison logic More nesting
Math.max() Clean and concise Requires nesting for more than two numbers
Ternary Operator Compact Less readable when nested

Frequently Asked Questions

How do I find the largest of three numbers in Java?

Use an if-else ladder with the logical AND operator or use nested Math.max() calls.


Can I use Math.max() to compare three numbers?

Yes.

Use nested calls:

Math.max(a, Math.max(b, c))

What happens if two numbers are equal?

Using >= correctly handles equal values and still returns one of the largest numbers.


Can I solve this without if-else?

Yes.

You can use nested Math.max() calls or a nested ternary operator.


How do I find the largest among many numbers?

Store the values in an array and use:

  • A loop

  • Java Streams

  • Collections.max() for collections


What is the time complexity?

All four methods perform only a fixed number of comparisons.

Time Complexity: O(1)


Why is the logical AND operator used?

A number must be greater than or equal to every other number.

Therefore, all comparison conditions must be true simultaneously.


Can I find the smallest number the same way?

Yes.

Replace >= with <= or use Math.min() instead of Math.max().


Does the ternary operator produce the same result?

Yes.

It is simply a shorter version of the if-else logic.


Is this question commonly asked in interviews?

Yes.

It is frequently asked in beginner Java interviews because it tests relational operators, logical operators, and conditional statements.


Math.max() is generally preferred because it is concise, readable, and relies on the standard Java library.


Can these methods be used with floating-point numbers?

Yes.

All four approaches work with double and float values because Java supports comparison operators and Math.max() for floating-point types.