Courses / Complete Java Course by codesofrohan / Conditional Statements / Modern Switch Expression in Java

Modern Switch Expression in Java

The traditional switch statement works, but modern Java provides a cleaner way of writing switch logic.

The biggest difference you'll notice is that instead of writing:

case 18:

and then using:

break;

we can use an arrow:

case 18 ->

This modern syntax makes switch code shorter and also allows a switch to produce a value.

Let's understand both ideas with the same real-world scenario.


Modern Switch Syntax

Suppose we have:

int age = 18;

Using the traditional switch, we would write:

switch (age) {

    case 18:
        System.out.println("Go and apply for driving license");
        break;

    case 60:
        System.out.println("It's time to retire");
        break;

    default:
        System.out.println("I have nothing to say about you");
}

With the modern syntax, we can write:

switch (age) {

    case 18 ->
        System.out.println("Go and apply for driving license");

    case 60 ->
        System.out.println("It's time to retire");

    default ->
        System.out.println("I have nothing to say about you");
}

Notice the difference.

Instead of:

case 18:

we have:

case 18 ->

And we don't need to write break.

Once the matching case is executed, Java doesn't continue executing the following cases in the same way as the traditional colon-based form.


What If We Need Multiple Lines?

Sometimes a case needs to perform more than one operation.

In that situation, we can use curly braces:

switch (age) {

    case 18 -> {
        System.out.println("Go and apply for the license");
        System.out.println("You can now apply");
    }

    case 60 ->
        System.out.println("It's time to retire");

    default ->
        System.out.println("I have nothing to say about you");
}

So the arrow syntax isn't limited to a single line. A block can be used when we need multiple statements.


Handling Multiple Values With One Case

Here's another useful feature of the modern switch syntax.

Suppose you want both 61 and 62 to produce the same result.

With the modern syntax, you can write:

case 61, 62 -> System.out.println("You are retired");

You don't need to create two separate blocks.

If age is 61, Java prints:

You are retired

If age is 62, Java prints the same thing.

You can extend this further:

case 61, 62, 63, 64, 65 ->
    System.out.println("You are retired");

Now all these values share the same result.

This is particularly useful when several possible values should lead to exactly the same action.


Switch Can Also Return a Value

Now we come to one of the most useful parts of the modern switch syntax.

A switch can be used as an expression, meaning it can produce a value that we can store in a variable.

Let's take a practical example.

Imagine a college wants to show the percentage range associated with a student's grade.

The user enters a grade:

A

and the program should tell them:

80 to 100

For example:

  • A → 80 to 100

  • B → 60 to 80

  • C → 50 to 60

  • D → 30 to 40

  • E → Failed, below 30

The program can take the grade as input and then use a modern switch expression.


Taking the Grade as Input

First, we can take a character from the user:

import java.util.Scanner;

Scanner scan = new Scanner(System.in);

char grade = scan.next().charAt(0);

Now suppose the user enters:

A

We can use that value in our switch.


Using yield to Return a Value

For a switch expression that needs a block and needs to produce a value, yield can be used.

For example:

String result = switch (grade) {

    case 'a' -> {
        yield "80 to 100";
    }

    case 'b' -> {
        yield "60 to 80";
    }

    case 'c' -> {
        yield "50 to 60";
    }

    case 'd' -> {
        yield "30 to 40";
    }

    case 'e' -> {
        yield "Failed, below 30";
    }

    default -> {
        yield "You have entered wrong grade";
    }
};

Here, the switch expression produces a value.

That value is stored inside:

String result

Then we can simply print:

System.out.println(result);

If the user enters:

a

the switch reaches:

case 'a'

and:

yield "80 to 100";

produces the value:

80 to 100

That value gets stored in result.

So:

System.out.println(result);

prints:

80 to 100

The important idea here is:

yield provides the value that the switch expression produces.


Handling Uppercase and Lowercase Grades

There is another practical situation.

What if the user enters:

A

instead of:

a

We can handle both values as part of the same case:

case 'a', 'A' -> {
    yield "80 to 100";
}

Similarly:

case 'b', 'B' -> {
    yield "60 to 80";
}

Now both lowercase and uppercase input can produce the same result.

We can do the same thing for the remaining grades:

case 'c', 'C' -> {
    yield "50 to 60";
}

case 'd', 'D' -> {
    yield "30 to 40";
}

case 'e', 'E' -> {
    yield "Failed, below 30";
}

And for anything else:

default -> {
    yield "You have entered wrong grade";
}

This is much cleaner than writing separate conditions for every uppercase and lowercase value.


Modern Switch as a Cleaner Alternative

Let's compare the two approaches.

Traditional syntax:

case 61:
    System.out.println("You are retired");
    break;

case 62:
    System.out.println("You are retired");
    break;

Modern syntax:

case 61, 62 -> System.out.println("You are retired");

The modern version is shorter and makes the intention much clearer.

Similarly, the traditional switch requires break when using the colon-based form, while the arrow form doesn't require it.


One Important Difference

There are two ideas to remember:

Traditional Switch Statement

switch (age) {

    case 18:
        System.out.println("Go and apply for driving license");
        break;
}

It uses:

:
break

Modern Switch Expression

switch (age) {

    case 18 ->
        System.out.println("Go and apply for driving license");
}

It uses:

->

and doesn't require break.

It can also produce a value:

String result = switch (grade) {

    case 'A' -> "80 to 100";

    default -> "Wrong grade";
};

When a block is used and we need to explicitly provide the resulting value, we can use:

yield

Final Takeaway

Modern switch syntax gives us a cleaner way to work with multiple fixed values.

Instead of writing a large else-if ladder or using the traditional switch syntax with repeated break statements, we can use arrow labels:

case 18 ->

We can also group multiple values:

case 61, 62, 63 ->

And when we need the switch to produce a value, we can use it as a switch expression:

String result = switch (grade) {
    // cases
};

With blocks, yield tells the switch expression which value it should produce.

So the three important things to remember from the modern syntax are:

->       → cleaner case syntax
,        → multiple values for the same case
yield    → return a value from a switch expression

This modern approach makes switch-based code more concise and readable, especially when several values need to be handled in a similar way.

🔖 Bookmark saved successfully!