Java中switch的参数类型

  @Test
    public void testSwitch() {
        byte b = 3;
        short s = 1;
        int i = 2;                    //byte、short会隐试的转换为int类型,所以switch参数类型可以接收byte、short、int
        long l = 1;                    //而long类型不能隐式的转换为int类型,所以switch参数类型不能接受long
        char c = '1';                
        String str = "hello";        //JDK1.7(包含1.7)之前的版本不支持char类型和String类型
        switch(i) {
            case 1: {
                System.out.println("选择一");
                break;
            }
            case 2: {
                System.out.println("选择二");
                break;
            }
            default : {
                System.out.println("其他");
                break;
            }
            
        }
    }

 

posted on 2018-03-23 15:27  温馨世界  阅读(2474)  评论(0编辑  收藏  举报