If /else,if/else, if/else… Instead. Take the switch code below

[Java] Plain text view copy code? int a = 10; switch(a){ case 5: System.out.println(“5”); break; case 3: System.out.println(“3”); break; case 1: System.out.println(“1”); break; default: System.out.println(“default”); break; } We can use if/else,if/else… Replace it with the following code:

[Java] Plain text view copy code? int a = 10; if(a == 5){ System.out.println(“5”); }else if( a==3 ){ System.out.println(“3”); }else if( a==1 ){ System.out.println(“1”); }else{ System.out.println(“default”); } Logically, if/else, if/else if/else, ternary operators, and switch can all be replaced by if, but using different syntax is more concise, and switch has better performance when there are many conditions. But why is the Switch performing better? So this is the bottom line of conditional judgment.

The execution of the program is eventually translated into a sequence of instructions to execute, the CPU has an instruction indicator, the indicator points to the instruction to execute, the CPU to execute the corresponding instruction according to the indicator. After an instruction is executed, the instruction indicator automatically points to the next instruction next to it. But there are special instructions, called jump instructions, that modify the next direction of the instruction indicator, causing the CPU to jump to a specific place to execute. There are two kinds of jump instructions, one is conditional jump, the other is unconditional jump. If a condition is met, the system jumps to the specified location. If the condition is not met, the system continues to the specified location. If the condition is unconditional, the system directly jumps to the specified location. And the if else underneath is actually converted to these jump instructions. For example: [Java] Plain text view copy code? 1 2 3 4 5 int x=10; [/align]if(x==10) {system.out.println (” dark “); } system.out. println(” podcasts “); [Java] plain text view copy code? 1: int x = 10; 2: conditional jump: if x==10, jump to line 4; 3: unconditional jump: jump to line 7; 4: {5: system.out.println (” dark horse programmer “); 6:} 7: system.out.println (“传智 的 podcasts “); So if you look at this and you’re wondering why there’s an unconditional jump here. Is it ok without this unconditional jump? The answer is no, because without the unconditional jump, the “dark horse programmer” inside the braces will execute the conditional jump regardless of whether the conditional jump is full or not. Let’s analyze if there is no unconditional jump: jump to braces if x==10. If x! =10, the CPU continues to execute the next instruction, and the next instruction is the one inside the braces. If x==0, the unconditional jump will be skipped and executed directly in curly braces. If x! =10, then continue to execute the next instruction, and the next instruction is an unconditional jump, it will jump to line 7 print the wisdom podcast. Sometimes, the compiler will translate the following situation, [JavaScript] plain text view copy code? 1: int x = 10; 2: Conditional jump: if x! =10, skip to line 6; 3: {4: system.out.println (” dark horse programmer “); 5:} [align=left] 6: system.out.println (“传智 地 “); In this way, the unconditional jump instruction is not required. The specific translation depends on the compile-time implementation. In the case of a single if, the unconditional jump instruction may not be required, but slightly more complex cases are required. [Java] Plain text view copy code? int a = 10; [/align]if(a == 5){ System.out.println(“5”); }else if( a==3 ){ System.out.println(“3”); }else if( a==1 ){ System.out.println(“1”); }else{ System.out.println(“default”); } system.out. println(” podcasts “); [Java] Plain text view copy code? 1: int a = 10; 2: conditional jump: if a==5, jump to line 4; 3: unconditional jump: jump to line 7; 4: {5: system.out.println (“5”); } 7: conditional jump: if a==3, jump to line 9; 8: Unconditional jump: jump to line 12; 9: {10: system.out.println (“3”); } 12: conditional jump: if x==1, jump to line 14; 13: Unconditional jump: jump to line 17; 14: {15: system.out.println (“1”); } 17: conditional jump: if x! =1&&x! =3&&x! =5, skip to line 19; 18: Unconditional jump: jump to line 22; 19: {20: system.out.println (“default”); Other code if, if/else, if/else if/else, ternary operators are all converted to conditional and unconditional jumps. But switch is different. The switch is compiled in such a way that it may be translated as a jump instruction if there are few branches. However, if there are many branches, using conditional jumps can make many comparisons, which is inefficient, so the compiler uses a more efficient method called jump tables. A jump table is a mapping table that stores the value of a case and the address to jump to, like: Why is a jump table more efficient? Because, the values must be integers and sorted by size. The types of switch values can be byte,short, int, char, enumeration, and String. Byte /short/int is an integer, char is an integer, and enum types have integers. String is converted to an integer when used for switch.) Integers sorted by size can use efficient binary lookup. The program source code in the case value arrangement do not seek to sort, the compiler will automatically sort. If the values are contiguous, the jump table will be optimized to an array, so you don’t even need to look for it. The value is the subscript index of the array, and the jump address can be found directly based on the value. Even if the values are not consecutive, but the numbers are dense and not far off, the compiler might optimize for an array of jump tables with no values pointing to the default branch. This can be verified using a decompilation tool, as shown in the following example:

Above is a switch we prepared to judge the source code. We generate class files after compilation, and use decompiler tools to restore the class files into source code. See below:

This is enough to say that if the case values are consecutive, or if the values are not consecutive, but the numbers are dense and the difference is not much, the compiler may also optimize for an array of jump tables with no values pointing to the default branch, and the jump table array is

Summary: Any logical structure that can be represented with switch can be represented with ifelse. However, if there are a lot of conditional branches judged, it is recommended to use switch more, because switch has high execution efficiency. This is because when the switch is compiled, its jump instruction will be compiled into an array, so it can jump instruction in efficient ways such as look-up table method and dichotomy search.