0

I've installed jdk 17 and try to compile this code:

    private static String getText(int number) {
        return switch (number) {
            case 1, 2 -> yield "one or two";
            case 3 -> yield "three";
            case 4, 5, 6 -> yield "four or five or six";
            default -> yield "unknown";
        };
    }

It doesn't compile, if I remove the yield keyword, then it gets compile. Where does the code get wrong?

Immanuel Kant
  • 517
  • 3
  • 8
  • My question: why my code doesn't get compiled? Where does it get wrong? – Immanuel Kant Aug 07 '22 at 02:54
  • 1
    Your code doesn’t work because it doesn’t follow Java’s syntax rules. Java could have been designed to allow this, but it wasn’t. This is like asking “why is `i int = 10;` not allowed but `int i = 10;` is? See https://meta.stackoverflow.com/questions/293815/is-it-subjective-to-ask-about-why-something-wasnt-implemented-in-the-language/293819#293819 for why questions like this are not answerable. – Sweeper Aug 07 '22 at 03:03

0 Answers0