java虚拟机中的指令---switch

1 篇文章 0 订阅

先看如下java源代码

public int testSwitch(){
		int i=3;
		switch(i){
			case 1:  return 1;
			case 2:  return 2;
			case 3:  return 3;
			default: return -1;
		}
	}


通过javap命令可以看到该源码被编码后的字节码如下:

   0:   iconst_3
   1:   istore_1
   2:   iload_1
   3:   tableswitch{ //1 to 3
                1: 28;
                2: 30;
                3: 32;
                default: 34 }
   28:  iconst_1
   29:  ireturn
   30:  iconst_2
   31:  ireturn
   32:  iconst_3
   33:  ireturn
   34:  iconst_m1
   35:  ireturn

java虚拟机使用tableswitch来处理switch语句

首先 push int constant 3 

 存储变量i,变量i是第一个,所以是istore_1 第0个是this

然后再加载i,进行tableswitch判断

当i是1时,将执行第28行,再执行29行退出

退出进将返回常量1,也就是iconst_1,  后面的一样

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值