Java中switch对整型/字符型/字符串型具体实现细节
2024-08-27 06:23:40
转自:http://www.hollischuang.com/archives/61
Java7中switch中支持的数据类型有: byte short int char String类型
其实switch是只支持一种数据类型,那就是整型:
1. byte short int 本身就是整型,可以直接按照整型进行比较
2. char的字符会依照ascii表,转换为对应的整型,然后进行switch条件选择
3. String的字符串会 先将字符串转换为hash值, 然后再对字符串进行equals方法比较, 以此来完成switch的条件选择
因为有ascii表存在,所以char类型和整数型可以进行转换, 也就是char类型可以直接赋值为整数型,整数型也可以直接赋值为字符型(无法转换的情况还没去归纳)
都说代码知识都是敲出来的,于是每种情况我都对比了下,如下图:
字节类型代码
这是字节类型的java源码,因为有字码表的存在,所以整数类型的变量也可以直接赋值字符
package compiler.demobyte; public class Demo01 { public static void main(String[] args) {
byte byt1 = 'a'; switch (byt1) {
case 'a':
System.out.println("hello");
break;
case 'b':
System.out.println("world");
break;
default:
break;
}
byte byt2 = 97;
switch (byt2) {
case 97:
System.out.println("hello");
break;
case 98:
System.out.println("world");
break;
default:
break;
}
}
}
这是.class文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package compiler.demobyte; public class Demo01 {
public Demo01() {
} public static void main(String[] args) {
byte byt1 = 97;
switch(byt1) {
case 97:
System.out.println("hello");
break;
case 98:
System.out.println("world");
} byte byt2 = 97;
switch(byt2) {
case 97:
System.out.println("hello");
break;
case 98:
System.out.println("world");
} }
}
short类型代码
short类型与byte类型是一样的
package compiler.demoshort; public class Demo01 { public static void main(String[] args) {
short sht = 'a';
switch (sht) {
case 'a':
System.out.println("hello");
break;
case 'b':
System.out.println("world");
break;
default:
break;
} short sht2 = 97;
switch (sht2) {
case 97:
System.out.println("hello");
break;
case 98:
System.out.println("world");
break;
default:
break;
}
}
}
这是.clas文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package compiler.demoshort; public class Demo01 {
public Demo01() {
} public static void main(String[] args) {
short sht = 97;
switch(sht) {
case 97:
System.out.println("hello");
break;
case 98:
System.out.println("world");
} short sht2 = 97;
switch(sht2) {
case 97:
System.out.println("hello");
break;
case 98:
System.out.println("world");
} }
}
int类型也是一样的,没有进行测试
char类型代码
package compiler.demochar; public class Demo01 { public static void main(String[] args) {
char ch = 'a';
switch (ch) {
case 'a':
System.out.println("hello");
break;
case 'b':
System.out.println("world");
break;
default:
break;
} char ch2 = 97;
switch (ch2) {
case 97:
System.out.println("hello");
break;
case 98:
System.out.println("world");
break;
default:
break;
} }
}
生成.class文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package compiler.demochar; public class Demo01 {
public Demo01() {
} public static void main(String[] args) {
char ch = 97;
switch(ch) {
case 97:
System.out.println("hello");
break;
case 98:
System.out.println("world");
} char ch2 = 97;
switch(ch2) {
case 97:
System.out.println("hello");
break;
case 98:
System.out.println("world");
} }
}
String 类型代码
String会稍微特殊一些,分两步进行转换:
第一步: 先将字符串转换为哈希值,进行条件选择. 然后因为存在哈希值碰撞的问题, 所以在条件选择成功后, 又使用了equals方法进行内容的比较.
哈希值的表现形式是 int整型
所以实质上: String类型也是视为是int整型的比较.
java代码如下:
package compiler.demostring; public class Demo01 { public static void main(String[] args) {
String str = "wor";
switch (str) {
case "hello":
System.out.println("hello");
break;
case "world":
System.out.println("world");
break;
default:
break;
}
}
}
.clas文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package compiler.demostring; public class Demo01 {
public Demo01() {
} public static void main(String[] args) {
String str = "wor";
byte var3 = -1;
switch(str.hashCode()) {
case 99162322:
if (str.equals("hello")) {
var3 = 0;
}
break;
case 113318802:
if (str.equals("world")) {
var3 = 1;
}
} switch(var3) {
case 0:
System.out.println("hello");
break;
case 1:
System.out.println("world");
} }
}
综上所述: 又回归到我们的结论: 其实switch是只支持一种数据类型,那就是整型.
最新文章
- ThinkPHP整合支付宝担保交易
- hdu 1348 Wall (凸包)
- 执行mount命令时找不到介质或者mount:no medium found的解决办法
- Spark Streaming揭秘 Day24 Transformation和action图解
- mvc Routing特性优化
- C#对html的操作
- Android Screen Monitor使用
- PHP Server Nginx 安装
- 包装类和基本类型区别?(integer和int取值范围一样大)
- thrift小试--C++
- Debian stretch更换国内源
- C++类的大小计算
- python:函数的高级特性
- plsql注册-转
- 单页面vue引入百度统计的使用方法!
- Dapper 的输出参数使用示范
- git clone Google的代码失败的解决方法
- g++多文件编译
- Spring整合Hibernate的XML文件配置,以及web.xml文件配置
- 使用sass与compass合并雪碧图(一)