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

转载于:https://www.cnblogs.com/Yiran-shampin/p/9473780.html

Java中switch对整型/字符型/字符串型具体实现细节相关推荐

  1. C语言中字符型和字符串型的对比

    C语言中字符型和字符串型的对比 字符型:     C语言中字符用单引号括起来,存储方式以ASCII编码二进制形式存储,占用一个字节     如:'a','b','c','A'等     注意:在C语言 ...

  2. 解析java中的字面量和字符类型

    解析java中的字面量和字符类型 1.字面量含义 固定不变的量,我们人为所给的一些数据.例如77和88都是整型字面量,1.88和1.99F是浮点型字面量,'中'是字符型字面量,"dfguyf ...

  3. 品味性能之道十一:JAVA中switch和if性能比较

    通常而言大家普遍的认知里switch case的效率高于if else.根据我的理解而言switch的查找类似于二叉树,if则是线性查找.按照此逻辑推理对于对比条件数目大于3时switch更优,并且对 ...

  4. java中switch用法举例范围_Java中Switch用法代码示例

    一.java当中的switch与C#相比有以下区别 注:在java中switch后的表达式的类型只能为以下几种:byte.short.char.int(在Java1.6中是这样), 在java1.7后 ...

  5. 字符型常量与字符串型常量

    形式上: 1. 字符常量是单引号括起来的一个字符 2. 字符串常量是双引号括起来的0个或多个字符 含以上: 1. 字符常量相当于一个整型值(ASCII值),可以参加表达式运算 2. 字符串常量代表一个 ...

  6. Java中switch都可以支持哪些数据类型

    Java中switch都可以支持哪些数据类型 在JDK1.5之前,switch循环只支持byte short char int四种数据类型. JDK1.5 在switch循环中增加了枚举类与byte ...

  7. 2021-12-27 Java String contains() 方法用于判断字符串中是否包含指定的字符或字符串。用.toLowerCase().contains忽视大小写。

    一.public boolean contains(CharSequence chars) contains() 方法用于判断字符串中是否包含指定的字符或字符串. 二.返回值        如果包含指 ...

  8. Java 中 switch 的用法

    Java 中 switch 的用法 1.switch 中的表达式的数据的数据类型为 byte, short, int, char, String(jdk > 1.7支持 String 类型) 2 ...

  9. java string设置编码_详解Java中String类型与默认字符编码

    为什么写这个 至于为什么要写这个,主要是一句mmp一定要讲,绕了一上午,晕死 Java程序中的中文乱码问题一直是一个困扰程序员的难题,自己也不例外,早在做项目时就遇到过很多编码方式的坑,当时想填来着, ...

最新文章

  1. select * from table with(nolock)
  2. 意识到自己的无知这就是进步
  3. 在eclipse中使用svn
  4. PHP学习笔记六【方法-递归】
  5. 【解决】ERROR in xxx.js from UglifyJs
  6. [react] 如何更新组件的状态?
  7. 需求管理与分析——需求池
  8. 手机闪存速度排行_2020年双十二3000-4000元高性价比手机推荐!
  9. 表单修改php参数,php – 使用参数修改symfony表单的url
  10. linux命令iconv_linux命令系列之iconv
  11. 【Java】JavaSocket编程开发聊天室-服务器端部分
  12. Java DataStore 封装代码
  13. 大三开学,百度面试感受
  14. Laravel sms短信验证码
  15. 美团java面试经历_美团面试经历+答案
  16. 快速搭建个人在线书库,随时随地畅享阅读!
  17. 数组的reduce的妙用之处
  18. Ubuntu20.04LTS 安装配置
  19. INFOR-CRB开发教程
  20. XX大学学生选课系统需求规格说明书

热门文章

  1. write solid code 零散(原文)
  2. 64位IIS(IIS6/IIS7)上跑Asp + Jet.Oledb的设置要点
  3. 《SQL Server 2012 T-SQL基础》读书笔记 - 1.背景
  4. Extjs鼠标长按事件(实现长按按钮触发事件的方法:mousedown、mouseup)
  5. Java学习—— for循环
  6. SQL设置语言,返回中文”星期几”格式
  7. Sql Server 数据分页
  8. 444牛X的日常口语
  9. Apache POI和EasyExcel 第三集:Apache POI的Excel大数据量写入(分为03版的xls、07版的xlsx、升级版SXSSF)
  10. halcon与c#联合编程的方法