Discuss / Java / yield 我觉的这样理解清晰点

yield 我觉的这样理解清晰点

Topic source

Zlstg

#1 Created at ... [Delete] [Delete and Lock User]
public class Main {
    public static void main(String[] args) {
        String fruit = "orange";
        int opt = switch (fruit) {
            case "apple" :
                yield 1;
            case "pear", "mango" :
                yield 2;
            default :
                int code = fruit.hashCode();
                yield code; // switch语句返回值
        };
        System.out.println("opt = " + opt);
    }
}

  • 1

Reply