Switch Syntax
switch
As Java built-in keywords, but actually used in the project is relatively small. About switch
, still there are some mysteries of.
To what switch, I have the if-else
Indeed, the project used switch
a relatively small main reason is that its effects could be if-else
replaced Moreover, switch
restrictions on the type, but also hindered the switch
further use.
Take a look at switch
the syntax:
switch(exp){
case exp1:
break;
case exp2:
break;
default:
break;
}
Wherein exp
the type is limited to: byte ,short , int , char,
its packaging, and enumeration and String
(JDK1.7)
Why have these restrictions?
If we say that switch
the function and if-else
the same, then the meaning of its existence where?
The answer is: switch
and if-else
the time in the design, there is a certain difference in performance.
Look at the code:
public class Test {
public static void switchTest(int a) {
switch (a) {
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
default:
System.out.println("3");
break;
}
}
}
javap -c Test.class
The results are as follows:
public static void switchTest(int);
Code:
0: iload_0
1: lookupswitch { // 2
1: 28
2: 39
default: 50
}
...
There is some code omitted.
It can be found switch
through the lookupswitch
instructions. Then the lookupswitch
command is doing it?
In Java se8 described in the document you may probably know:
switch
It can be compiled into two instructions
lookupswitch
: Whenswitch
thecase
sparse time, the use of the commandint
valuescase
one by one comparison, until it finds the correspondingcase
(look here, can be optimized for the binary search)tableswitch
: Whenswitch
acase
more intensive when usedcase
value asswitch
the index, can be found in the case of the corresponding time complexity is O (1) iscase
(the HashMap is analogous)
And documentation as well as a description:
Java virtual machine
tableswitch
andlookupswitch
instructions onlyint
data is valid. Becausebyte
,char
or orshort
the operation inside the values promotedint
, so itsswitch
expression evaluates to compile one type, if it is calculated to be the same typeint
. If thechooseNear
method is to use a type of writing, the type of useshort
will generate the same Java virtual machine instructionsint
. Other types must be reduced to a digital typeint
for use in aswitch
.
Now, we should be able to understand why switch
keywords will have restrictions on the type, because the switch
keyword is translated is limited to the type int , int as to why, I guess it should be based on considerations of performance and complexity to achieve it .
Types other than int
We understand byte,shor,char,int
that can be used as switch
the type, and then look enumerationString
public static void switchTest(String a) {
switch (a) {
case "1":
System.out.println("1");
break;
case "2":
System.out.println("2");
break;
default:
System.out.println("3");
break;
}
}
Compiled Test.class. IDEA dragged decompile obtained the following code:
public static void switchTest(String a) {
byte var2 = -1;
switch(a.hashCode()) {
case 49:
if (a.equals("1")) {
var2 = 0;
}
break;
case 50:
if (a.equals("2")) {
var2 = 1;
}
}
switch(var2) {
case 0:
System.out.println("1");
break;
case 1:
System.out.println("2");
break;
default:
System.out.println("3");
}
}
You can see, JDK7 supported String
type is by getting String
to choose the hashCode, that is, in essence, is int. Why String
be so dry? It depends on String
a constant class.
In order to prevent hash collisions, the insurance code more were
equals
judged.
Let's look atEnum
public static void switchTest(Fruit a) {
switch (a) {
case Orange:
System.out.println("Orange");
break;
case Apple:
System.out.println("Apple");
break;
default:
System.out.println("Banana");
break;
}
}
Compiled Test.class. IDEA dragged decompile obtained the following code:
public static void switchTest(Fruit a) {
switch(1.$SwitchMap$com$dengchengchao$Fruit[a.ordinal()]) {
case 1:
System.out.println("Orange");
break;
case 2:
System.out.println("Apple");
break;
default:
System.out.println("Banana");
}
}
It can be seen enumerate support switch
more simple, direct by order of enumeration can be as relevantcase
In short:
switch
Logically, design, thanif-else
to be fast, but in 99.99% of cases, they are about the same performance, unlesscase
a huge amount of branches, butcase
under excessive fragmentation, the general should consider using a multi-state reconstruction.switch
While supportingbyte,int,short,char,enum,String
but essentially allint
other compiler just help you make a syntactic sugar optimize it.
Respect for labor, reproduced indicate the source
If you thought that it was good, welcome attention to the micro-channel public number: Yat tour Java, every day from time to time publish articles related to Java advanced, thanks to attention