首页 > 其他分享 >switch(jdk8)

switch(jdk8)

时间:2023-11-17 16:01:19浏览次数:29  
标签:case default System break switch jdk8 println out

 

本质

字节码

int类型

  1 int = 4 byte

public static void switchTest(int a) {
        switch (a) {
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            default:
                System.out.println("3");
                break;
        }
    }


字节码:
 0 iload_0
 1 lookupswitch 2
	1:  28 (+27)
	2:  39 (+38)
	default:  50 (+49)
28 getstatic #3 <java/lang/System.out>
...

  

  switch的case <3 时,使用的lookupswitch指令实现;

 

lookupswitch

Access jump table by key match and jump  通过key匹配访问

 

The key must be of type int and is popped from the operand stack.   

  key 必须是int类型
The key is compared against the match values. 
If it is equal to one of them, then a target address is calculated by adding the corresponding offset to the address of the opcode of this lookupswitch instruction. 
If the key does not match any of the match values, the target address is calculated by adding default to the address of the opcode of this lookupswitch instruction. 

  如果没有匹配的key,将会使用default;  
Execution then continues at the target address.

 

 

public static void switchTest(int a) {
        switch (a) {
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            case 3:
                System.out.println("3");
                break;
            default:
                System.out.println("default");
                break;
        }
    }


字节码:
 0 iload_0
 1 tableswitch 1 to 3	1:  28 (+27)
	2:  39 (+38)
	3:  50 (+49)
	default:  61 (+60)
28 getstatic #3 <java/lang/System.out>
...

  

  switch的case >=3 时,使用的tableswitch 指令实现;

 

tableswitch

Access jump table by index and jump  通过index访问

 

The index must be of type int and is popped from the operand stack.   index必须是int类型
If index is less than low or index is greater than high, then a target address is calculated by adding default to the address of the opcode of this tableswitch instruction.
Otherwise, the offset at position index - low of the jump table is extracted.
The target address is calculated by adding that offset to the address of the opcode of this tableswitch instruction.
Execution then continues at the target address.

 

byte类型

  1byte = 8 bit

public static void switchTestByte(byte a) {
        switch (a) {
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            default:
                System.out.println("3");
                break;
        }
    }


字节码:
 0 iload_0
 1 lookupswitch 2
	1:  28 (+27)
	2:  39 (+38)
	default:  50 (+49)
28 getstatic #3 <java/lang/System.out>
...

  

short类型

  1 short = 2 byte

public static void switchTestShort(short a) {
        switch (a) {
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            default:
                System.out.println("3");
                break;
        }
    }


字节码:

 0 iload_0
 1 lookupswitch 2
	1:  28 (+27)
	2:  39 (+38)
	default:  50 (+49)
28 getstatic #3 <java/lang/System.out>
...

 

char类型  

public static void switchTestChar(char a) {
        switch (a) {
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            default:
                System.out.println("3");
                break;
        }
    }


字节码:
 0 iload_0
 1 lookupswitch 2
	1:  28 (+27)
	2:  39 (+38)
	default:  50 (+49)
28 getstatic #3 <java/lang/System.out>
...

  

String类型

public static void switchTestString(String a) {
        switch (a) {
            case "a":
                System.out.println("a");
                break;
            case "b":
                System.out.println("b");
                break;
            default:
                System.out.println("default");
                break;
        }
    }


字节码:
  0 aload_0
  1 astore_1
  2 iconst_m1
  3 istore_2
  4 aload_1
  5 invokevirtual #4 <java/lang/String.hashCode>   //String 的hashCode值
  8 lookupswitch 2
	97:  36 (+28)           // "a"的Hashcode值
	98:  50 (+42)           // "b"的Hashcode值
	default:  61 (+53)
 36 aload_1
 37 ldc #2 <a>
 39 invokevirtual #5 <java/lang/String.equals>
 42 ifeq 61 (+19)
 45 iconst_0
 46 istore_2
 47 goto 61 (+14)
 50 aload_1
 51 ldc #6 <b>
 53 invokevirtual #5 <java/lang/String.equals>
 56 ifeq 61 (+5)
 59 iconst_1
 60 istore_2
 61 iload_2    

...

  

  String类型本质:用的hashcode值,也是int类型

 

Enum类型

enum MyEnum {
        A,B,C
    }


public static void switchTestEnum(MyEnum myEnum) {
        switch (myEnum) {
            case A:
                System.out.println("a");
                break;
            case B:
                System.out.println("b");
                break;
            default:
                System.out.println("default");
                break;
        }
    }


字节码:
 0 getstatic #4 <classtest/SwitchTest$1.$SwitchMap$classtest$SwitchTest$MyEnum>
 3 aload_0
 4 invokevirtual #5 <classtest/SwitchTest$MyEnum.ordinal>  //枚举的ordinal方法
 7 iaload
 8 lookupswitch 2
	1:  36 (+28)
	2:  47 (+39)
	default:  58 (+50)
36 getstatic #6 <java/lang/System.out>
...

  

  enum本质 :enum类型的ordinal方法(当前枚举值在枚举类型的序号0下标起始);

 

为什么switch会有类型限制?

  字节码 lookupswitch、tableswitch指令 要求key/index 必须是int类型

 

标签:case,default,System,break,switch,jdk8,println,out
From: https://www.cnblogs.com/anpeiyong/p/17838960.html

相关文章

  • 【Java基础】Java中switch的多种写法
    Java中switch的多种写法代码需求:键盘录入一个数字(代表星期几),判断是工作日还是休息日switch最基础写法 publicstaticvoidswitchTest(){while(true){System.out.println("请输入:");Scannersc=newScanner(System.in);......
  • 记录jdk17相对于jdk8增加的一下主要语法糖和新特性
    jdk17发布已经好久了,作为java的长期支持版本,引入了许多有趣且实用的新特性。这些特性不仅提高了开发效率,还增强了语言的表现力和安全性。并且是SpringBoot3.0以后版本的硬性要求,之后势必会是java开发的主要版本。经过我个人的测试体验,在Java17环境中运行使用早期Ja......
  • 【转】JDK8 升级 JDK11 最全实践干货来了 | 京东云技术团队
    原文地址:JDK8升级JDK11最全实践干货来了|京东云技术团队作者:京东云开发者1.前言截至目前(2023年),Java8发布至今已有9年,2018年9月25日,Oracle发布了Java11,这是Java8之后的首个LTS版本。那么从JDK8到JDK11,到底带来了哪些特性呢?值得我们升级吗?而且升级过程会......
  • VB.Net Switch 语句
    VB.NetSwitch语句SelectCasenumColumnsCase0numColumns=numColumns+1'SomecodeCase1numColumns=numColumns+1'SomedifferentcodeCase2numColumns=numColumns+1'S......
  • 性能测试复习准备——linux环境下安装jdk8
     先在根目录下创建目录: /soft/jdk8  ——用于放上传进来的软件包;       然后通过左边的上传按钮,把包上传到 /soft/jdk8下面; 然后在根目录下面创建/evir/jdk8目录,用户放解压后的软件包;tar-zxvffile.tar.gz-C/path/to/destination   tar......
  • 注意break除了用于中断循环以及switch语句,还可以用于标签化语句的中断
    请问以下JS代码的输出结果以及变量i的值是?vari=100;functionfoo(){bbb:try{console.log("position1");returni++;}finally{breakbbb;}console.log("position2");returni;}foo();Aposition1、posit......
  • vue2 switch 将文字显示在按钮内部
    el-elementplus已经实现了该功能了,其实主要就是改样式。效果图样式修改<el-switchv-model="value"active-color="#13ce66"active-text="在读"inactive-text="毕业"inactive-color="#ff4949"></el-switch>.el-switch_......
  • HashMap---jdk8
    概述HashtablebasedimplementationoftheMapinterface.Thisimplementationprovidesalloftheoptionalmapoperations,andpermits<tt>null</tt>valuesandthe<tt>null</tt>key.(The<tt>HashMap</tt>classis......
  • 循环:switch
    1.switch循环 switch(Type){case1:if(answerCount>1){isCorrect=false;remark+="单选题只能有一个答案;";......
  • (七)C#编程基础复习——Switch语句
    switch语句有点类似ifelseif语句,都可以根据表达式执行某个语句块,使用之前要遵循以下几个条件:switch语句中表达式的值必须是一个整型或者枚举类型;在一个switch语句中可以有任意数量的case雨具,每个case关键字后面要跟一个表达式比较的值和一个冒号;case关键字后面的值必须与swi......