zoukankan      html  css  js  c++  java
  • JAVA编程思想(第四版)学习笔记----4.8 switch(知识点已更新)

    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的基础上进行编写的,所以对现在来说当时的书中介绍的难免有过时之处。

    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)
  • 相关阅读:
    在Eclipse中制作SSH配置文件提示插件
    JsonUtil
    jQuery Callback 函数
    jcifs包实现域认证的单点登录带来了个奇怪的问题
    jQuery AJAX 函数
    java反射技
    jcifs的NTLMHTTP验证及所犯错误
    jQuery CSS 函数
    JCIFS
    JCIFS NTLM HTTPAuthenticationJCIFS使用NTLMHTTP认证
  • 原文地址:https://www.cnblogs.com/gl-developer/p/6021035.html
Copyright © 2011-2022 走看看