配送网站开发,公司优化是什么意思,开发购物网站描述,lnmp wordpress 换域名JDK8以后的语法没学习了#xff0c;现在时代发展这么快#xff0c;所以得加紧时间学习了。JDK12只有一个特性就是switch语法#xff0c;算是比较容易学习的一个版本吧。总体来说就是三部分内容。具体内容可以看JEP-325的内容。
箭头语法 每个case可以放箭头了。以下是一个例… 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);}}
}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);}
}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);}
}