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

  1. switch (integral-selector) {
  2. case integral-value1:
  3. statement;
  4. break;
  5. case integral-value12:
  6. statement;
  7. break;
  8. default:
  9. statement;
  10. }

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

java官方手册中找到switch语句的说明中,已经明确指出了 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)

JAVA编程思想(第四版)学习笔记----4.8 switch(知识点已更新)的更多相关文章

  1. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings

    Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...

  2. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(四)之Operators

    At the lowest level, data in Java is manipulated using operators Using Java Operators An operator ta ...

  3. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(三)之Everything Is an Object

    ---恢复内容开始--- Both C++ and Java are hybird languages. A hybird language allow multiple programming st ...

  4. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(二)之Introduction to Objects

    The genesis of the computer revolution was a machine. The genesis of out programming languages thus ...

  5. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information

    Runtime type information (RTTI) allow you to discover and use type information while a program is ru ...

  6. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十二)之Error Handling with Exceptions

    The ideal time to catch an error is at compile time, before you even try to run the program. However ...

  7. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects

    To solve the general programming problem, you need to create any number of objects, anytime, anywher ...

  8. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十)之Inner Classes

    The inner class is a valuable feature because it allows you to group classes that logically belong t ...

  9. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(九)之Interfaces

    Interfaces and abstract classes provide more structured way to separate interface from implementatio ...

  10. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Polymorphism

    Polymorphism is the third essential feature of an object-oriented programming language,after data ab ...

随机推荐

  1. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

  2. 提高代码质量系列之二:重构小技巧——if篇

    前言: if,相信是童鞋们使用的最频繁的关键字了,而且很多时候,我们使用的if都是在无意识的状态下随手而为.键入if,两下回车(我使用了resharper,可以自动编排if的格式),再信手写下我们需要 ...

  3. TODO:Go语言goroutine和channel使用

    TODO:Go语言goroutine和channel使用 goroutine是Go语言中的轻量级线程实现,由Go语言运行时(runtime)管理.使用的时候在函数前面加"go"这个 ...

  4. 【原】彻底解决WPS弹出热点广告、WPS购物图标的办法

    一直用WPS,但一直有一个问题迟迟没有解决,那就是讨厌的WPS广告问题! 每次开机都会自动在托盘上闪烁图标:“WPS购物”和“WPS热点”! 用自定义托盘图标隐藏都不管用,自动又会给改回来!这简直是流 ...

  5. C#设计模式-访问者模式

    一. 访问者(Vistor)模式 访问者模式是封装一些施加于某种数据结构之上的操作.一旦这些操作需要修改的话,接受这个操作的数据结构则可以保存不变.访问者模式适用于数据结构相对稳定的系统, 它把数据结 ...

  6. 一步步构造自己的vue2.0+webpack环境

    前面vue2.0和webpack都已经有接触了些(vue.js入门,webpack入门之简单例子跑起来),现在开始学习如何构造自己的vue2.0+webpack环境. 1.首先新建一个目录vue-wk ...

  7. 制作自己的MVC框架(三)——应用

    一.数据库操作 目前封装了两种数据库,“MongoDB”和“MySQL”,用到了一次接口“IDatabase.php”. namespace library\db; interface IDataba ...

  8. Android 开发环境搭建以及工具(不断更新)

    学习android需要学习的编程知识 https://wiki.cyanogenmod.org/w/Doc:_Development_Resources 从http://source.android. ...

  9. php后台编辑关联数据

    数据库中两张表: info表中"民族"关联了nation表中的"code". php通过后台编辑info表中民族显示成用户可看懂及可直接修改的选项. 新建xin ...

  10. 理解CSS前景色和透明度

    前面的话 颜色的出现让网页不再只是黑白,运用好颜色设计,能让网页增色不少.一个网页给人们留下的第一印象实际上就是它的整体颜色.关于如何设置颜色,请移步CSS的6种颜色模式.实际上,颜色的应用主要分为前 ...