• Java12~14 switch语法


      JDK8以后的语法没学习了,现在时代发展这么快,所以得加紧时间学习了。JDK12只有一个特性就是switch语法,算是比较容易学习的一个版本吧。总体来说就是三部分内容。具体内容可以看JEP-325的内容。

    箭头语法

      每个case可以放箭头了。以下是一个例子:

    public class SwitchDemo {
        public static void main(String[] args) throws IOException {
            System.out.println("请输入一个正整数:");
            int r = System.in.read();
            switch (r) {
                case '1', '2' -> System.out.println("小于3");
                case '3' -> System.out.println("等于3");
                default -> System.out.println("大于3");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    switch作为表达式

      现在switch语句块可以作为表达式赋值给变量了。在以前是需要每个case块里写上赋值语句。对于这个新特性,我写了一个示例代码:

    public class SwitchDemo2 {
        public static void main(String[] args) throws IOException {
            System.out.println("请输入一个正整数:");
            int r = System.in.read();
            String day = switch (r) {
                case '1' -> "Monday";
                case '2' -> "Tuesday";
                case '3' -> "Wednesday";
                case '4' -> "Thursday";
                case '5' -> "Friday";
                case '6' -> "Saturday";
                case '7' -> "Sunday";
                default -> null;
            };
            System.out.println(day);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    break/yield返回值

      在case块里,如果返回值需要复杂运算的,可以先写运算语句,再用break关键字来返回。但是这个关键字马上在新版本JDK13中被取消了。JEP-325中有这个例子:

    Most switch expressions will have a single expression to the right of the “case L ->” switch label. In the event that a full block is needed, we have extended the break statement to take an argument, which becomes the value of the enclosing switch expression.
    int j = switch (day) {
      case MONDAY -> 0;
      case TUESDAY -> 1;
      default -> {
        int k = day.toString().length();
        int result = f(k);
        break result;
      }
    };

      但是这个代码只能在JDK12能编译通过,在JDK13中需要把break换成yield关键字。所以这样写才能编译通过:

    public class SwitchDemo3 {
        public static void main(String[] args) throws IOException {
            System.out.println("请输入一个正整数:");
            int r = System.in.read();
            String s= "Foo";
            String day = switch (r) {
                case '1' -> "Monday";
                case '2' -> "Tuesday";
                case '3' -> "Wednesday";
                case '4' -> "Thursday";
                case '5' -> "Friday";
                case '6' -> "Saturday";
                case '7' -> "Sunday";
                default -> {
                    System.err.println("一个星期只有7天");
                    yield "不知道怎么说";
                }
            };
            System.out.println(day);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    现有TiDB集群扩展pump/drainer作为binlog文件落地
    word的分隔符和分页符
    聊城办理ISO认证企业须知
    码云gitee+宝塔webhook实现同步代码
    easyPoster一个基于uniapp的canvas绘制海报库支持绘制图表到海报中
    朴素贝叶斯算法分类
    怎么样零代码零成本搭建个人网站?
    批量控制教程-Ansible管理windows
    抽象工厂模式 rust和java的实现
    Canal + MySQL + Zookeeper + Kafka 数据实时同步
  • 原文地址:https://blog.csdn.net/m0_66201040/article/details/136765427