博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA编程思想(第四版)学习笔记----4.8 switch(知识点已更新)
阅读量:4448 次
发布时间:2019-06-07

本文共 1147 字,大约阅读时间需要 3 分钟。

switch语句和if-else语句不同,switch语句可以有多个可能的执行路径。在第四版java编程思想介绍switch语句的语法格式时写到:

switch (integral-selector) {        case integral-value1:             statement;             break;        case integral-value12:            statement;             break;        default:            statement;        }

其中integral-selector(整数选择因子)是一个能产生整数值的表达式。并且说明选择因子必须是一个int或者char那样的整数值,或者是一个enum枚举类型。由于java编程思想第四版是在JDK1.5的基础上进行编写的,所以对现在来说当时的书中介绍的难免有过时之处。

在中,已经明确指出了 A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types , the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer。其中enum枚举类型是JDK1.5中新增加的新特性,而switch对于String字符串的支持则是在JDK7以后。

在使用JDK7版本的环境进行编程时,使用switch语句的选择因子,如果不符合规范(比如float类型变量)会产生提示: Cannot switch on a value of type float. Only convertible int values, strings or enum variables are permitted 由于, byte,short,char 都可以隐含转换为 int 所以int类型变量中也包含了byte,short,char类型变量。

总结:

可以用作switch选择因子的数据类型有:

  • char,byte,short,int以及他们的包装类Character,Byte,Short,Integer
  • enmu枚举 (since jdk1.5)
  • String字符串(since jdk1.7)

 

转载于:https://www.cnblogs.com/gl-developer/p/6021035.html

你可能感兴趣的文章
项目中使用的axios
查看>>
c++学习-继承
查看>>
[转]SQL Server 性能调优(io)
查看>>
设计模式学习-每日一记(6.原型模式)
查看>>
不已0开头的数字正则
查看>>
21.优先队列的实现
查看>>
HTML撑起浮动子元素得父元素高度
查看>>
LeetCode--018--四数之和(java)
查看>>
Redis消息队列
查看>>
电商网站架构设计
查看>>
http://jingyan.baidu.com/article/4dc40848e7b69bc8d946f127.html
查看>>
WCF netTcp配置
查看>>
单例类
查看>>
python 正则表达式 提取网页中标签的中文
查看>>
2015武大校赛
查看>>
LA 2531 The K-league 最大流
查看>>
从零开始学习前端JAVASCRIPT — 6、JavaScript基础DOM
查看>>
Edit显示行号
查看>>
取得字符串中指定的字符str[]
查看>>
delphi TOpenDialog
查看>>