0Pricing
Java Academy · Lesson

The yield Keyword

Produce values from switch blocks.

The yield Keyword is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When One Line Is Not Enough

An arrow branch like case 1 -> 42; returns a value directly. But what if a branch needs several statements before producing its value? That is where yield comes in.

The yield Keyword

Inside a block branch { }, the yield keyword supplies the value that the switch expression produces. Think of it as return, but for a switch branch.

public class Main {
    public static void main(String[] args) {
        int n = 3;
        int result = switch (n) {
            case 3 -> {
                int squared = n * n;
                yield squared;
            }
            default -> 0;
        };
        System.out.println(result);
    }
}

yield Inside a Block

You can run any logic in the block before calling yield. The last thing the block does should be to yield a value.

public class Main {
    public static void main(String[] args) {
        int amount = 250;
        String label = switch (amount / 100) {
            case 0 -> "small";
            default -> {
                System.out.println("Computing label...");
                yield "large";
            }
        };
        System.out.println(label);
    }
}

Mixing Arrow and Block Branches

A single switch expression can mix simple arrow branches with block branches that use yield. Use whichever fits each case.

public class Main {
    public static void main(String[] args) {
        int code = 2;
        String msg = switch (code) {
            case 1 -> "One";
            case 2 -> {
                String s = "T" + "wo";
                yield s;
            }
            default -> "Other";
        };
        System.out.println(msg);
    }
}

Computation Before yield

Block branches are ideal when a value needs calculation. Here we sum digits before yielding the total.

public class Main {
    public static void main(String[] args) {
        int category = 1;
        int value = switch (category) {
            case 1 -> {
                int sum = 0;
                for (int i = 1; i <= 5; i++) {
                    sum += i;
                }
                yield sum;
            }
            default -> 0;
        };
        System.out.println(value);
    }
}

yield vs return

yield exits only the switch branch and gives back a value. return would exit the entire method. Inside a switch expression block, use yield.

public class Main {
    static int classify(int n) {
        int rank = switch (n) {
            case 0 -> 0;
            default -> {
                yield (n > 0) ? 1 : -1;
            }
        };
        return rank;
    }
    public static void main(String[] args) {
        System.out.println(classify(-7));
    }
}

yield in the Old Colon Style

The colon-style switch can also be an expression using yield instead of break. Each labeled section yields its value.

public class Main {
    public static void main(String[] args) {
        int day = 2;
        String name = switch (day) {
            case 1: yield "Mon";
            case 2: yield "Tue";
            default: yield "Other";
        };
        System.out.println(name);
    }
}

Conditional yield

A block branch can contain if logic and yield different values on different paths. Every path must yield a value.

public class Main {
    public static void main(String[] args) {
        int temp = 30;
        String advice = switch (temp / 10) {
            case 3 -> {
                if (temp > 35) {
                    yield "Very hot";
                }
                yield "Warm";
            }
            default -> "Mild";
        };
        System.out.println(advice);
    }
}

Yielding Objects

yield works with any type, including objects. Here a branch builds and yields a StringBuilder result as a String.

public class Main {
    public static void main(String[] args) {
        int count = 3;
        String stars = switch (count) {
            case 0 -> "";
            default -> {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < count; i++) {
                    sb.append("*");
                }
                yield sb.toString();
            }
        };
        System.out.println(stars);
    }
}

Readability Tip

Prefer simple arrow branches when a single value works. Reach for a block and yield only when a branch truly needs extra statements. This keeps your switch tidy.

public class Main {
    public static void main(String[] args) {
        int level = 2;
        int xp = switch (level) {
            case 1 -> 100;
            case 2 -> {
                int base = 100;
                yield base * 2;
            }
            default -> 0;
        };
        System.out.println(xp);
    }
}

A Complete Example

Putting it together: an HTTP-style status mapper that uses both arrow and block branches.

public class Main {
    public static void main(String[] args) {
        int status = 404;
        String message = switch (status / 100) {
            case 2 -> "Success";
            case 4 -> {
                String base = "Client error ";
                yield base + status;
            }
            case 5 -> "Server error";
            default -> "Unknown";
        };
        System.out.println(message);
    }
}

Quick Check

Inside a multi-statement switch expression branch, which keyword supplies the resulting value?

Recap

You learned the yield keyword:

  • yield produces a value from a block branch
  • It is like return but for a switch branch only
  • Mix arrow and block branches freely
  • Every code path in a block must yield
  • Use blocks only when extra statements are needed

Frequently asked questions

Is the “The yield Keyword” lesson free?

Yes — the full text of “The yield Keyword” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “The yield Keyword”?

Produce values from switch blocks. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The yield Keyword” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arrow-Style switch
  2. switch as an Expression
  3. The yield Keyword
  4. Multi-Label Cases
← Back to Java Academy