Java: switch lambda-like syntax
Posted ascertain
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java: switch lambda-like syntax相关的知识,希望对你有一定的参考价值。
The switch expression has an additional lambda-like syntax and it can be used not only as a statement, but also as an expression that evaluates to a single value.
With the new lambda-like syntax, if a label is matched, then only the expression or statement to the right of the arrow is executed; there is no fall through
package com.example.prom; import java.util.Scanner; public class D public static void main(String[] args) Scanner scanner = new Scanner(System.in); String next = scanner.next(); byte result = switch (next) case "A" -> 1; case "B" -> 2; default -> throw new IllegalStateException("Unexpected value: " + next); ; System.out.println("result = " + result);
package com.example.prom; import java.util.Scanner; public class D public static void main(String[] args) Scanner scanner = new Scanner(System.in); String next = scanner.next(); int result; switch (next) case "A" -> result = 1; case "B" -> result = 2; case "C" -> result = 3; System.out.println("3!"); default -> System.err.println("Unexpected value: " + next); result = -1; System.out.println("result = " + result);
yield In the situation when a block is needed in a case, yield can be used to return a value from it
package com.example.prom; import java.util.Scanner; public class D public static void main(String[] args) Scanner scanner = new Scanner(System.in); String next = scanner.next(); var result = switch (next) case "A" -> 1; case "B" -> 2; case "C", "D", "E" -> System.out.println("3!"); yield 3; // return default -> throw new IllegalStateException("Unexpected value: " + next); ; System.out.println("result = " + result);
protected double calculator(char operator, double x, double y) return switch (operator) case \'+\' -> x + y; case \'-\' -> x - y; case \'*\' -> x * y; case \'/\' -> if (y == 0) throw new IllegalArgumentException("Can\'t divide by 0"); yield x / y; default -> throw new IllegalArgumentException("Unknown operator `%s`".formatted(operator)); ;
java基础之switch
switch 语句由一个控制表达式和多个case标签组成。
switch 控制表达式支持的类型有byte、short、char、int、enum(Java 5)、String(Java 7)。
switch-case语句完全可以与if-else语句互转,但通常来说,switch-case语句执行效率要高。
default在当前switch找不到匹配的case时执行。default并不是必须的。
一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见break。
switch语法格式
switch (表达式) { case 条件1: 语句1; break; case 条件2: 语句2; break; ... default: 语句; }
分支语句先对表达式进行求值,然后依次匹配条件1,条件2... ...当表达式的值于条件相匹配时,执行对应的语句,如果没有对应对条件,则执行default语句。
byte:
1 byte b1=12; 2 switch(b1){ 3 case 1: 4 System.out.println("wrong"); 5 break; 6 7 case 2: 8 System.out.println("wrong2"); 9 break; 10 11 case 12: 12 System.out.println("right"); 13 break; 14 15 default: 16 System.out.println("all wrong"); 17 }
int:
1 int num1=12; 2 switch(num1){ 3 case 1: 4 System.out.println("wrong"); 5 break; 6 7 case 2: 8 System.out.println("wrong2"); 9 break; 10 11 case 12: 12 System.out.println("right"); 13 break; 14 15 default: 16 System.out.println("all wrong"); 17 }
char:
1 char c1=‘a‘; 2 switch (c1) { 3 case ‘a‘: 4 System.out.println("a is right"); 5 break; 6 7 case ‘b‘: 8 System.out.println("b is right"); 9 break; 10 11 case ‘c‘: 12 System.out.println("c is right"); 13 break; 14 default: 15 break; 16 }
string:
1 String str1="123"; 2 switch (str1) { 3 case "111": 4 System.out.println("111 is right"); 5 break; 6 7 case "112": 8 System.out.println("112 is right"); 9 break; 10 11 case "123": 12 System.out.println("123 is right"); 13 break; 14 default: 15 System.out.println(" no right"); 16 break; 17 }
Enum:
1 static enum E{ 2 A,B,C,D 3 } 4 public static void main(String[] args) { 5 E e=E.C; 6 7 switch (e) { 8 case A: 9 System.out.println("A is right"); 10 break; 11 12 case B: 13 System.out.println("B is right"); 14 break; 15 16 case C: 17 System.out.println("C is right"); 18 break; 19 20 case D: 21 System.out.println("D is right"); 22 break; 23 default: 24 System.out.println("no right"); 25 break; 26 }
以上是关于Java: switch lambda-like syntax的主要内容,如果未能解决你的问题,请参考以下文章
c语言的switch语句和java的switch语句有啥不同吗?