转自: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是只支持一种数据类型,那就是整型.

最新文章

  1. ThinkPHP整合支付宝担保交易
  2. hdu 1348 Wall (凸包)
  3. 执行mount命令时找不到介质或者mount:no medium found的解决办法
  4. Spark Streaming揭秘 Day24 Transformation和action图解
  5. mvc Routing特性优化
  6. C#对html的操作
  7. Android Screen Monitor使用
  8. PHP Server Nginx 安装
  9. 包装类和基本类型区别?(integer和int取值范围一样大)
  10. thrift小试--C++
  11. Debian stretch更换国内源
  12. C++类的大小计算
  13. python:函数的高级特性
  14. plsql注册-转
  15. 单页面vue引入百度统计的使用方法!
  16. Dapper 的输出参数使用示范
  17. git clone Google的代码失败的解决方法
  18. g++多文件编译
  19. Spring整合Hibernate的XML文件配置,以及web.xml文件配置
  20. 使用sass与compass合并雪碧图(一)

热门文章

  1. python - 递归 二分法
  2. 苹果手机的SB系列(1)听不懂人话的sir
  3. windows下使用 fdfs_client 上传文件
  4. Springmvc对就jdbc封装的操作
  5. java-面向对象(公元2017-6-28)
  6. 初遇sass的两个小问题
  7. WinHex18.4算法分析
  8. Java实现视频转码或压缩demo.
  9. python之路-数据类型(方法)
  10. python之路-模块初识