Tag: switch statement

if else vs java中的switch性能

我想知道使用if语句或switch之间是否存在任何效率差异。 例如: if(){ //code } else if(){ //code } else{ //code } 我相信程序需要去检查所有if语句,即使第一个if语句是真的。 switch(i){ case 1: //code break; case 2: //code break; 但是在交换机中,有一个break命令。 我接近了吗? 如果没有,你能解释一下它们之间的效率差异吗?

使用Maven编译JDK12预览function

使用JDK / 12 EarlyAccess Build 10 ,JEP-325 Switch Expressions已作为JDK中的预览function集成。 表达式的示例代码(如在JEP中一样): Scanner scanner = new Scanner(System.in); Day day = Day.valueOf(scanner.next()); switch (day) { case MONDAY, TUESDAY -> System.out.println(“Back to work.”) ; case WEDNESDAY -> System.out.println(“Wait for the end of week…”) ; case THURSDAY,FRIDAY -> System.out.println(“Plan for the weekend?”); case SATURDAY, SUNDAY -> System.out.println(“Enjoy the holiday!”); } […]

为什么switch语句比Java 7中的String更快?

在Java 7中 , string对象可以在switch语句的表达式中。 有人可以从官方文档中解释以下声明吗? Java编译器通常使用String对象生成比使用链式if-then-else语句更高效的字节码。

如何在java中的while循环中设置switch语句

我想在while循环中进行切换,在每个switch语句中断while循环停止并请求输入如F,R,C,Q。下面的语句有效,但语句不会中断。 请帮忙 public static void main(String[] args) throws IOException { // start both with 1 point int goodTotal = 50; int monTotal = 50; // input switch statement while (goodTotal > 0 && monTotal > 0) { System.out.print(“Type a letter: “); System.out.println(“\n”); System.out.print(“F: Go out and Fight “); System.out.println(“\n”); System.out.print(“R: Rest “); System.out.println(“\n”); System.out.print(“C: Check […]

Java 7 String switch反编译:意外指令

我已经反编译了一个使用新的Java 7字符串切换function的非常简单的类。 class上: public class StringSwitch { public static void main(String[] args) { final String color = “red”; switch (color) { case “red”: System.out.println(“IS RED!”); break; case “black”: System.out.println(“IS BLACK”); break; case “blue”: System.out.println(“IS BLUE”); break; case “green”: System.out.println(“IS GREEN”); break; } } } 对这个类运行Java 7“javap”,会生成一组有趣的指令( 这里提供了完整的反汇编代码): public static void main(java.lang.String[]); flags: ACC_PUBLIC, ACC_STATIC Code: […]

switch语句中的最终变量大小写

final int a = 1; final int b; b = 2; final int x = 0; switch (x) { case a:break; // ok case b:break; // compiler error: Constant expression required } /* COMPILER RESULT: constant expression required case b:break; ^ 1 error */ 为什么我会遇到这种错误? 如果我做了final int b = 2 ,一切正常。

替代Java中的嵌套切换语句

所以今天我编写了一个方法,结合了嵌套switch语句的使用,代码看起来相当干净简洁,但我被告知嵌套的switch语句通常不是最好的方法,因为它们可能会让人感到困惑。您添加的switch语句。 以下是我的代码的示例: EnumOne enumOne; EnumTwo enumTwo = null; EnumTwo enumThree = null; switch (enumOne) { case CASE_ONE: switch (enumTwo){ case A: enumTwo = EnumTwo.B; break; case C: enumTwo = EnumTwo.D; break; default: break; } switch (enumThree) { case AA: enumThree = EnumTwo.BB; break; case CC: enumThree = EnumTwo.DD; break; default: break; } break; case CASE_TWO: […]

在Java中使用regex for switch-statement

void menu() { print(); Scanner input = new Scanner( System.in ); while(true) { String s = input.next(); switch (s) { case “m”: print(); continue; case “s”: stat(); break; case “[AZ]{1}[az]{2}\\d{1,}”: filminfo( s ); break; case “Jur1”: filminfo(s); break; //For debugging – this worked fine case “q”: ; return; } } } 好像我的正则表达式已关闭或我在case语句中没有正确使用它。 我想要的是一个字符串:开头只有一个大写字母,后面跟着两个小写字母,后跟至少一个数字。 我已经检查了正则表达式API并尝试了三种变体(贪婪,不情愿和占有量词)而不知道它们的正确用法。 还检查了String的方法,但未找到与我的需求相关的方法。

在Switch Case Java中编译时间常量用法

case使用规则说: case表达式必须求值为Compile Time Constant 。 case(t)表达式必须与switch(t)的表达式相同,其中t是类型(String)。 如果我运行此代码: public static void main(String[] args) { final String s=new String(“abc”); switch(s) { case (s):System.out.println(“hi”); } } 它给出了编译错误: “case expression must be a constant expression”另一方面,如果我尝试使用final String s=”abc”; ,它工作正常。 据我所知, String s=new String(“abc”)是对位于堆上的String对象的引用。 而s本身就是一个编译时常量。 这是否意味着final String s=new String(“abc”); 是不是编译时间常数?

Switch Statement给出了不兼容类型错误

我正在尝试编译,我收到此错误: enigma/Rotor.java:30: incompatible types found : java.lang.String required: int switch(name){ 1 error 为什么我收到此错误? 我如何解决它? 它在包中,我似乎无法弄明白。 这是代码: String label; Rotor(){;} Rotor(String name){ switch(name){ case “B”: conversion_chart = B; break; case “C”: conversion_chart = C; break; case “I”: conversion_chart=I; notch = NOTCH[0]; break; case “II”: conversion_chart=II; notch = NOTCH[1]; break; case “III”: conversion_chart=III; notch = NOTCH[2]; break; […]