前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Javap分析Java代码里的static final的工作原理

使用Javap分析Java代码里的static final的工作原理

作者头像
Jerry Wang
发布2020-07-06 23:47:45
2220
发布2020-07-06 23:47:45
举报

Created by Jerry Wang, last modified on Oct 05, 2016

I have written the following test code:

I would like to test the difference with “static int” and “static final int”. Use javap to decompile .class file:

The result is listed below:

According to JVM instruction list explanation in wiki: https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings sipush 545: push int value 545 to stack putstatic #16: put the stack value 545 to static class field number3

aload_0: load a reference onto the stack from local variable 0 invokespecial: invoke instance method on object objectref and puts the result on the stack (might be void); the method is identified by method reference index in constant pool ldc: push a constant #index from a constant pool (String, int or float) onto the stack Here we can know the fact that the value of result 512 * 623 has already been calculated during compilation and stored in constant pool index #29. istore_1: store int value into variable 1 getstatic: get a static field value of a class, where the field is identified by field reference in the constant pool index imul: multiply two integers new: create new object of type identified by class reference in constant pool index dup: duplicate the value on top of the stack

You might wonder how we can know each # represents. In order to get the table you can use javap -v instead:

We can also know that the "Value: " + product1 + " , " + product2 is coverted automatically by JVM to create a new StringBuilder instance and call append method to achieve string concatenation.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Created by Jerry Wang, last modified on Oct 05, 2016
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档