java基础之switch关键字

switch关键字

1、switch语句的完整格式

switch(值){
     case 值1:
         java语句;
         ......;
        java语句;
        break;
     case 值2:
         java语句;
         ......;
        java语句;
        break;
     case 值3:
         java语句;
         ......;
        java语句;
        break;
        
     ..........
      default:
        break;
}

2、注意事项

注意以下几个点:

1、break可以不写,但是这种使用方式是在上学的时候考试专门出的面试题;

2、default可以不写;这里只不过是这种给一个默认的值;

3、switch中的值在jdk8中只能够是int类型和字符串;(byte、short、char也可以使用,但是会进行自动类型转换);

4、switch中的值在和case中的值来进行比较的时候,使用的是“==”来进行比较的。

5、如果执行完成case中的java代码之后,没有遇到break,那么将会发生case穿透现象。(这里原理很简单)

3、switch语句的执行原理

执行原理:

1、拿switch中的值和case中的值进行"=="比较,如果匹配上了,然后执行java语句,遇到break语句结束当前程序;

2、如果没有匹配上,那么继续来匹配下一个case中的值2来进行比较,然后遇到了break语句,则当前程序终止进行操作;

3、如果最终都没有匹配上,那么default分支中的代码将会成功。

4、完整执行流程

看一下对应的完整的执行流程:

public class SwitchTestOne {
    public static void main(String[] args) {
        int i = 1;
        // 这里直接报错,不兼容的数据类型。这里只兼容int类型和字符串类型,所以需要使用起来的时候进行注意
        switch (i){
            case 1:
                System.out.println("hello,case 1");
                System.out.println("hello,case 1");
                System.out.println("hello,case 1");
                break;
            case 2:
                System.out.println("hello,case 2");
                System.out.println("hello,case 2");
                System.out.println("hello,case 2");
                break;

            case 3:
                System.out.println("hello,case 3");
                System.out.println("hello,case 3");
                System.out.println("hello,case 3");
                break;
            default:
                System.out.println("running default statement");
        }
    }
}

然后查看控制台打印输出语句:

hello,case 1
hello,case 1
hello,case 1

5、case穿透演示

下面来演示一下case穿透现象:

public class SwitchTestTwo {
    public static void main(String[] args) {
        int i = 1;
        // 这里直接报错,不兼容的数据类型。这里只兼容int类型和字符串类型,所以需要使用起来的时候进行注意
        switch (i){
            case 1:
                System.out.println("hello,case 1");
                System.out.println("hello,case 1");
                System.out.println("hello,case 1");
                // 去掉这里的break关键字,模拟造成case穿透现象
                // break;
            case 2:
                System.out.println("hello,case 2");
                System.out.println("hello,case 2");
                System.out.println("hello,case 2");
                break;

            case 3:
                System.out.println("hello,case 3");
                System.out.println("hello,case 3");
                System.out.println("hello,case 3");
                break;
            default:
                System.out.println("running default statement");
        }
    }
}

可以看到控制台输出内容:

hello,case 1
hello,case 1
hello,case 1
hello,case 2
hello,case 2
hello,case 2

5、字符串在switch中

然后看一下字符串类型的演示:

public class SwitchTestThree {
    public static void main(String[] args) {
        String i = "liguang1";
        // 这里直接报错,不兼容的数据类型。这里只兼容int类型和字符串类型,所以需要使用起来的时候进行注意
        switch (i){
            case "liguang":
                System.out.println("hello,case 1");
                System.out.println("hello,case 1");
                System.out.println("hello,case 1");
                // 去掉这里的break关键字,模拟造成case穿透现象
                // break;
            case "limeng":
                System.out.println("hello,case 2");
                System.out.println("hello,case 2");
                System.out.println("hello,case 2");
                break;

            case "lixiang":
                System.out.println("hello,case 3");
                System.out.println("hello,case 3");
                System.out.println("hello,case 3");
                break;
            default:
                System.out.println("running default statement");
        }
    }
}

6、不兼容的类型

public class SwitchTestFour {
    public static void main(String[] args) {
        Object ob = new Object();
        Long l = 2L;
        // 这里直接报错,不兼容的数据类型。这里只兼容int类型和字符串类型,所以需要使用起来的时候进行注意
        switch (ob){
        }
    }
}

从上面可以看到,除了int类型和字符串类型之外,那么其他的数据类型都是不支持的,爆出了数据不兼容的错误。

posted @ 2022-03-27 14:48  写的代码很烂  阅读(335)  评论(0编辑  收藏  举报