java吧 关注:1,301,255贴子:12,847,196
  • 0回复贴,共1

JAVA12发布了,更优雅的switch表达式,了解一下?省

取消只看楼主收藏回复

JAVA12发布了,更优雅的switch表达式,了解一下?
省略break的写法:
switch (today) {
case SUNDAY, SATURDAY -> System.out.println("I'm happy!");
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> System.out.println("I'm happy, too!!"); default -> System.out.println("I'm confused.");
}
直接赋值
String word = switch (num) {
case 1 -> "One";
case 2 -> "Two";
default -> { String result = String.format("Other (%d)", num); break result; }
};
当然也可以这样写,此时break的作用相当于return
String word = switch (num) {
case 1 : break "One";
case 2 : break "Two";
default : { String result = String.format("Other (%d)", num); break result; }
};


IP属地:上海来自Android客户端1楼2019-06-11 02:28回复