揭秘Java switch语句中的case穿透现象

news/2024/10/21 10:03:15/

揭秘Java switch语句中的case穿透现象

  • 1. switch 语句简介
  • 2. case穿透现象的原因
    • 关于 goto
  • 3. switch和if的区别
  • 4. 总结

导语:在 Java 开发中,我们经常使用switch语句来进行条件判断和分支选择。然而,有一个令人困惑的现象就是,当某个case语句没有加上break关键字时,程序会继续执行下一个case语句,这被称为case穿透现象。本文将揭秘case穿透现象的原因,并解释为何会出现这种行为。

1. switch 语句简介

在开始揭秘case穿透现象之前,我们先简单回顾一下switch语句的基本用法。switch语句用于根据变量的不同取值执行相应的代码块。其语法结构如下:

switch (expression) {case value1:// 执行代码块1break;case value2:// 执行代码块2break;...default:// 默认代码块
}

switch case支持的6种数据类型:switch 表达式后面的数据类型只支持byte、short、int整形类型、字符类型char、枚举类型和java.lang.String类型。

根据expression的值,程序会跳转到对应的case语句进行匹配并执行相应的代码块,直到遇到break关键字或者到达switch语句的结尾。

如果某个case语句没有break,程序会继续执行下一个case语句,这就是case穿透现象

我们看下面这个例子。

public class Test {public static void main(String[] args) {int i = 0;switch (i) {case 0:System.out.println("0");case 1:System.out.println("1");case 2:System.out.println("2");}}
}

打印结果:

0
1
2

2. case穿透现象的原因

按照惯用套路,看看字节码能不能给个答案。

javac编译javap查看

> javap -c -l Test.class
Compiled from "Test.java"
public class com.atu.algorithm.aTest.Test {public com.atu.algorithm.aTest.Test();Code:0: aload_01: invokespecial #1                  // Method java/lang/Object."<init>":()V4: returnLineNumberTable:line 8: 0public static void main(java.lang.String[]);Code:0: iconst_01: istore_12: iload_13: tableswitch   { // 0 to 20: 281: 362: 44default: 52}28: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;31: ldc           #3                  // String 033: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V36: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;39: ldc           #5                  // String 141: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V44: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;47: ldc           #6                  // String 249: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V52: returnLineNumberTable:line 10: 0line 11: 2line 13: 28line 15: 36line 17: 44line 19: 52
}

根据提供的字节码,我们来解释一下case穿透的情况。

在main方法中,通过tableswitch指令实现了一个switch语句。switch语句会根据值进行跳转,并执行对应的代码块。

在这个例子中,根据tableswitch指令的参数 {0 to 2}case的范围是从0到2。

  • switch的表达式的值为0时,程序会跳转到标签为28的位置,然后继续执行28标签处的代码块。
  • 为1时跳转到标号36代码处;
  • 为2时跳转到标号44代码处;
  • default则跳转到标号52代码处。

这不,答案就出来了,当case 0匹配了之后,直接跳转到标号28代码处开始执行,输出0,然后策马奔腾,一路下坡,顺序执行完后面所有代码,直到标号52 return,方法完执行完成,程序结束。

如果按照正常的思维,是不是case 0匹配之后,跳到28,执行完28、31、32输出0之后,就应该直接跳走,直接执行49。

那么,这个【跳走】用字节码应该怎么表示?

关于 goto

再写代码样例,这次我们在代码中给每个case都加上break

public class Test {public static void main(String[] args) {int i = 0;switch (i) {case 0:System.out.println("0");break;case 1:System.out.println("1");break;case 2:System.out.println("2");break;}System.out.println("Hello World");}
}

打印结果:

0
Hello World

重新编译,再来看看字节码。

Compiled from "Test.java"
public class com.atu.algorithm.aTest.Test {public com.atu.algorithm.aTest.Test();   Code:                                  0: aload_0                                                                  1: invokespecial #1                  // Method java/lang/Object."<init>":()V4: returnLineNumberTable:line 8: 0public static void main(java.lang.String[]);Code:0: iconst_01: istore_12: iload_13: tableswitch   { // 0 to 20: 281: 392: 50default: 58}28: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;31: ldc           #3                  // String 033: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V36: goto          5839: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;42: ldc           #5                  // String 144: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V47: goto          5850: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;53: ldc           #6                  // String 255: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V58: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;61: ldc           #7                  // String Hello World63: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V66: returnLineNumberTable:line 10: 0line 11: 2line 13: 28line 14: 36line 16: 39line 17: 47line 19: 50line 22: 58line 23: 66
}

如图,与第一次的字节码相比,在标号36、47都有了goto指令。如果case 0匹配成功,则跳到标号28执行,执行完代码块对应的31、33指令之后,执行36的goto指令跳转到标号58,这样就跳出了switch作用范围,case 1和2也不会被执行。

在Java字节码中,goto指令用于无条件跳转到指定的目标代码块。它可以实现程序的跳转和循环控制。

等等,怎么少了一个goto,在标号58的上方应该还有一个goto才对!

其实这就涉及到了编译器优化技术,最后一个goto也是跳转到标号58的指令,但没有goto下一步也一样顺序执行此行指令,所以这个goto被编译器视为无用代码进行了消除。

3. switch和if的区别

先用if实现上面switch逻辑。

public class Test {public static void main(String[] args) {int i = 0;if (i == 0) {System.out.println(0);} else if (i == 1) {System.out.println(1);} else if (i == 2) {System.out.println(2);}System.out.println("Hello World");}
}

编译成字节码

Compiled from "Test.java"
public class com.atu.algorithm.aTest.Test {public com.atu.algorithm.aTest.Test();Code:0: aload_01: invokespecial #1                  // Method java/lang/Object."<init>":()V4: returnLineNumberTable:line 8: 0public static void main(java.lang.String[]);Code:0: iconst_01: istore_12: iload_13: ifne          166: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;9: iconst_010: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V13: goto          4316: iload_117: iconst_118: if_icmpne     3121: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;24: iconst_125: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V28: goto          4331: iload_132: iconst_233: if_icmpne     4336: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;39: iconst_240: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V43: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;46: ldc           #4                  // String Hello World48: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V51: return
}

「ifne」和「if_icmpne」是Java字节码指令中的两个条件分支指令,用于在程序执行过程中进行条件判断并跳转到相应的代码块。它们的区别在于操作数类型和比较方式。

  • ifne:操作数类型为int,功能是当栈顶元素不等于零时,跳转到指定的代码块。
  • if_icmpne:操作数类型为int,当两个int类型的数值不相等时,跳转到指定的代码块。

从字节码也可以看出ifswitch的区别:

  • if条件和代码块的字节码是顺序的,switch条件和代码块是分开的;
  • if自动生成goto指令,switch只有加了break才生成goto指令。

4. 总结

  1. case穿透现象:指在switch语句中,当某个case语句没有break,程序会继续执行下一个case语句。
  2. case中的break作用是告诉前端编译器:「给每个case对应代码块的最后加上goto」。这样,执行完匹配上的代码之后,就可以略过后面的case代码块了。
  3. switch都支持哪些类型呢?
    • 基本数据类型:byte, short, char, int
    • 包装数据类型:Byte, Short, Character, Integer
    • 枚举类型:Enum
    • 字符串类型:String(Jdk 7+ 开始支持)

http://www.ppmy.cn/news/1184040.html

相关文章

Prompt-Tuning源码分析

Prompt-Tuning源码分析 源码 我们这里的代码解析以huggingface peft源码为主 从模型类结构可以看到&#xff0c;Prompt Tuning 只在输入层加入 prompt virtual tokens&#xff0c;其他地方均没有变化&#xff0c;具体可查看 PromptEmbedding 的源码。 伪代码示例 soft_prom…

【SPSS】基于RFM+Kmeans聚类的客户分群分析(文末送书)

&#x1f935;‍♂️ 个人主页&#xff1a;艾派森的个人主页 ✍&#x1f3fb;作者简介&#xff1a;Python学习者 &#x1f40b; 希望大家多多支持&#xff0c;我们一起进步&#xff01;&#x1f604; 如果文章对你有帮助的话&#xff0c; 欢迎评论 &#x1f4ac;点赞&#x1f4…

unocss和tailwindcss css原子引擎

第一种tailwindcss&#xff1a; tailwindcss官网 https://tailwindcss.com/docs/grid-column 基本介绍及优点分析 Tailwind CSS 中文文档 - 无需离开您的HTML&#xff0c;即可快速建立现代网站 PostCss 处理 Tailwind Css 基本流程 PostCSS - 是一个用 JavaScript 工具和插…

拉扎维模拟CMOS集成电路设计西交张鸿老师课程P2~5视频学习记录

目录 p2 p3 p4 p5 --------------------------------------------------------------------------------------------------------------------------------- p2 -----------------------------------------------------------------------------------------------------…

YOLOv5算法 | 万字长文带你深度解析yolov5s.yaml配置文件

前言&#xff1a;Hello大家好&#xff0c;我是小哥谈。配置文件yolov5s.yaml在YOLOv5模型训练过程中发挥着至关重要的作用&#xff0c;属于初学者必知必会的文件&#xff01;在YOLOv5-6.0版本源码中&#xff0c;配置了5种不同大小的网络模型&#xff0c;分别是YOLOv5n、YOLOv5s…

Element 多个Form表单 同时验证

一、背景 在一个页面中需要实现两个Form表单&#xff0c;并在页面提交时需要对两个Form表单进行校验&#xff0c;两个表单都校验成功时才能提交 所用技术栈&#xff1a;Vue2Element UI 二、实现效果 三、多个表单验证 注意项&#xff1a; 两个form表单&#xff0c;每个表单上…

机器视觉3D项目评估的基本要素及测量案例分析

目录 一. 检测需求确认 1、产品名称&#xff1a;【了解是什么产品上的零件&#xff0c;功能是什么】 2、*产品尺寸&#xff1a;【最大兼容尺寸】 3、*测量项目&#xff1a;【确认清楚测量点位】 4、*精度要求&#xff1a;【若客户提出的精度值过大或者过小&#xff0c;可以和客…

tomcat9~10猫闪退个人经验

java版本17与8 8版本有jre&#xff0c;java17没有jre 所以在java8版本中将jre和jdk路径一同添加环境是不会出现闪退的&#xff0c;tomcat9没有闪退 但是在10就闪退了&#xff0c;因为java版本太低 java17没有jre&#xff0c;但是可以通过一种方法添加jre到java17的目录 完…