• switch语法中break,default作用说明


    关于java中switch使用的一些说明
    switch(表达式)
    {
    case 常量表达式1:语句1;
    ....
    case 常量表达式2:语句2;
    default:语句;
    }
    default就是如果没有符合的case就执行它,default并不是必须的.
    case后的语句可以不用大括号.
    switch语句的判断条件可以接受int,byte,char,short,不能接受其他类型.
    如果使用long类型的话编译时会有错误产生,这点在使用上要注意,其他的数据类型都不行。 
    简单地说就是能够自动转换程int类型的数据类型才行。 
    而case是指switch小括号中的变量会出现且你想要处理的值,它除了可以是个整数、字符之外,还可以是一些简单的算术表达式,不过算数表达式的结果要满足刚刚所说的四种数据类型。一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见break,利用这一特性可以让好几个case执行统一语句
    Java代码  收藏代码
    1. 1.package flowcontrol;     
    2. 2.    
    3. 3.public class SwitchCase {     
    4. 4.    // first default     
    5. 5.    public static void testFirst(int i) {     
    6. 6.        switch (i) {     
    7. 7.        default:     
    8. 8.            System.out.println("default");// first default     
    9. 9.        case 1:     
    10. 10.            System.out.println("one");     
    11. 11.        case 2:     
    12. 12.            System.out.println("two");     
    13. 13.        case 3:     
    14. 14.            System.out.println("there");     
    15. 15.        }     
    16. 16.    }     
    17. 17.    
    18. 18.    // last default     
    19. 19.    public static void testLast(int i) {     
    20. 20.        switch (i) {     
    21. 21.        case 1:     
    22. 22.            System.out.println("one");     
    23. 23.        case 2:     
    24. 24.            System.out.println("two");     
    25. 25.        case 3:     
    26. 26.            System.out.println("there");     
    27. 27.        default:     
    28. 28.            System.out.println("default");// last default     
    29. 29.        }     
    30. 30.    }     
    31. 31.    
    32. 32.    // middle default     
    33. 33.    public static void testMiddle(int i) {     
    34. 34.        switch (i) {     
    35. 35.        case 1:     
    36. 36.            System.out.println("one");     
    37. 37.        case 2:     
    38. 38.            System.out.println("two");     
    39. 39.        default:     
    40. 40.            System.out.println("default");// middle default     
    41. 41.        case 3:     
    42. 42.            System.out.println("there");     
    43. 43.    
    44. 44.        }     
    45. 45.    }     
    46. 46.    
    47. 47.    public static void main(String[] args) {     
    48. 48.        // first default     
    49. 49.        testFirst(2);     
    50. 50.        System.out.println("------------------");     
    51. 51.        testFirst(9);     
    52. 52.    
    53. 53.        System.out.println("|||||||||||||||||||||||||||||||||||");     
    54. 54.    
    55. 55.        // last default     
    56. 56.        testLast(2);     
    57. 57.        System.out.println("----------------");     
    58. 58.        testLast(9);     
    59. 59.    
    60. 60.        System.out.println("|||||||||||||||||||||||||||||||||||");     
    61. 61.        // middle default     
    62. 62.        testMiddle(2);     
    63. 63.        System.out.println("----------------");     
    64. 64.        testMiddle(9);     
    65. 65.    
    66. 66.    }     
    67. 67.    
    68. 68.}    
    输入结果:
    Java代码  收藏代码
    1. 1.two     
    2. 2.there     
    3. 3.------------------     
    4. 4.default    
    5. 5.one     
    6. 6.two     
    7. 7.there     
    8. 8.|||||||||||||||||||||||||||||||||||     
    9. 9.two     
    10. 10.there     
    11. 11.default    
    12. 12.----------------     
    13. 13.default    
    14. 14.|||||||||||||||||||||||||||||||||||     
    15. 15.two     
    16. 16.default    
    17. 17.there     
    18. 18.----------------     
    19. 19.default    
    20. 20.there  

    看了结果,可以这样理解: 
    (1)switch语句关键地方是进入点,有了进入点没有break的情况下会执行到底 
    (2)没有匹配值的时候default就是进入点,进入default以后会和普通进入点一样,如果没有break继续执行下面语句 
    (3)如果有break 则是从进入点到 break中间的语句执行




  • 相关阅读:
    SSM学习笔记之Spring, SpringIoC, 注解, SpringAOP, Spring整合MyBatis
    Java学习笔记之网络编程
    Maven学习笔记之Mac环境下安装和配置Maven
    SSM学习笔记之MyBatis
    SSM学习笔记之SpringMVC
    ODP.NET 开发 出现 ORA12154: TNS:could not resolve the connect identifier specified 错误
    MEF开发资源
    Windows Store 应用程序开发示例资源
    QNAS MariaDB 远程登录配置
    Oracle通过DBLINK访问PG13
  • 原文地址:https://www.cnblogs.com/marcotan/p/4256899.html
Copyright © 2020-2023  润新知