Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
150 lines (123 loc) · 5.45 KB

File metadata and controls

150 lines (123 loc) · 5.45 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
Java 7switch的参数可以是String类型了这对我们来说是一个很方便的改进到目前为止switch支持这样几种数据类型:`byte` `short` `int` `char` `String` 。但是作为一个程序员我们不仅要知道他有多么好用还要知道它是如何实现的switch对整型的支持是怎么实现的呢对字符型是怎么实现的呢String类型呢有一点Java开发经验的人这个时候都会猜测switch对String的支持是使用equals()方法和hashcode()方法那么到底是不是这两个方法呢接下来我们就看一下switch到底是如何实现的
<!--more-->
### switch对整型支持的实现
下面是一段很简单的Java代码定义一个int型变量a然后使用switch语句进行判断执行这段代码输出内容为5那么我们将下面这段代码反编译看看他到底是怎么实现的
public class switchDemoInt {
public static void main(String[] args) {
int a = 5;
switch (a) {
case 1:
System.out.println(1);
break;
case 5:
System.out.println(5);
break;
default:
break;
}
}
}
//output 5
反编译后的代码如下
public class switchDemoInt
{
public switchDemoInt()
{
}
public static void main(String args[])
{
int a = 5;
switch(a)
{
case 1: // '\001'
System.out.println(1);
break;
case 5: // '\005'
System.out.println(5);
break;
}
}
}
我们发现反编译后的代码和之前的代码比较除了多了两行注释以外没有任何区别那么我们就知道,**switch对int的判断是直接比较整数的值**。
### switch对字符型支持的实现
直接上代码
public class switchDemoInt {
public static void main(String[] args) {
char a = 'b';
switch (a) {
case 'a':
System.out.println('a');
break;
case 'b':
System.out.println('b');
break;
default:
break;
}
}
}
编译后的代码如下: `public class switchDemoChar
public class switchDemoChar
{
public switchDemoChar()
{
}
public static void main(String args[])
{
char a = 'b';
switch(a)
{
case 97: // 'a'
System.out.println('a');
break;
case 98: // 'b'
System.out.println('b');
break;
}
}
}
通过以上的代码作比较我们发现对char类型进行比较的时候实际上比较的是ascii码编译器会把char型变量转换成对应的int型变量
### switch对字符串支持的实现
还是先上代码
public class switchDemoString {
public static void main(String[] args) {
String str = "world";
switch (str) {
case "hello":
System.out.println("hello");
break;
case "world":
System.out.println("world");
break;
default:
break;
}
}
}
对代码进行反编译
public class switchDemoString
{
public switchDemoString()
{
}
public static void main(String args[])
{
String str = "world";
String s;
switch((s = str).hashCode())
{
default:
break;
case 99162322:
if(s.equals("hello"))
System.out.println("hello");
break;
case 113318802:
if(s.equals("world"))
System.out.println("world");
break;
}
}
}
看到这个代码你知道原来字符串的switch是通过`equals()``hashCode()`方法来实现的。**记住switch中只能使用整型**,比如`byte`。`short`,`char`(ackii码是整型)以及`int`。还好`hashCode()`方法返回的是`int`,而不是`long`。通过这个很容易记住`hashCode`返回的是`int`这个事实仔细看下可以发现进行`switch`的实际是哈希值然后通过使用equals方法比较进行安全检查这个检查是必要的因为哈希可能会发生碰撞因此它的性能是不如使用枚举进行switch或者使用纯整数常量但这也不是很差因为Java编译器只增加了一个`equals`方法如果你比较的是字符串字面量的话会非常快比如abc” ==”abc”。如果你把`hashCode()`方法的调用也考虑进来了那么还会再多一次的调用开销因为字符串一旦创建了它就会把哈希值缓存起来因此如果这个`switch`语句是用在一个循环里的比如逐项处理某个值或者游戏引擎循环地渲染屏幕这里`hashCode()`方法的调用开销其实不会很大
以上就是关于switch对整型字符型和字符串型的支持的实现方式总结一下我们可以发现,**其实switch只支持一种数据类型那就是整型其他数据类型都是转换成整型之后在使用switch的。**
Morty Proxy This is a proxified and sanitized view of the page, visit original site.