Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between integer constants and integer variables in java

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what is the difference between integer constants and integer variables in java", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn "what is the difference between integer constants and integer variables in java".

Lines 5 and 9 of the code below define an integer variable and an integer constant, respectively:

Static final int number1 = 512

Static int number3 = 545

Java programmers know the difference between the two.

Let's decompile the .class file with javap and delve into the difference between integer variables and integer constants in Java.

Use the command line javap-c constant.ConstantFolding to view the bytecode decompiled from the .class file:

Results:

Descriptions of these bytecode instructions are described in wikipedia:

Wiki: https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings

We Java programmers don't need to memorize them all, we just need to put the page away and use it as a dictionary when we use it:

Sipush 545: put the integer 545 on the stack

Putstatic # 16:

Assign the value 545 on the stack to the static field of the current class.

So what does # 16 in putstatic # 16 mean?

When we decompile with the javap-v parameter, we can see the constant pool (Constant pool) of this class. Take a look at the blue highlighted line in the following picture:

Constant/ConstantFolding.number3:I

Description # 16 represents number3, a member of class constant.ConstantFolding, with type I.

At this point, these two lines of bytecode instructions are combined to actually correspond to the Java code we wrote:

Static int number3 = 545

Let's continue to analyze the bytecode decompiled by javap.

Aload_0: loads the introduction of a local variable with sequence number 0 on the stack

Invokespecial: calls the member method on the object instance. If there is a return value, the return value of the method is stored on the stack. The specific method called is identified by #, and the corresponding method name can be queried in the constant pool.

Ldc: loads the value of the constant code # on the constant pool from the constant pool onto the stack.

From the constant pool list in the figure below, we can see that the constant 318976 with sequence number # 29 is the product of integer constants number1 and integer constants. From this, we can see that the expression number1 * number2, because the two operands involved in the operation are modified into integer constants by STATIC and FINAL, so the product can be obtained at compile time, so the compiler calculates it at compile time and stores it in the variable pool with the serial number # 29.

So the integer variables do multiplication, and what is the corresponding bytecode?

Start with the code with serial number 3 in the following figure:

Getstatic # 16: load the static member # 16 of the class onto the stack. The corresponding member of # 16 is number3, with a value of 545.

Getstatic # 18: load the static member # 18 of the class onto the stack. The corresponding member of # 18 is number4, with a value of 619.

Imul: performs the multiplication of two integers on the stack.

Istore_2: save the results to local variable 2.

At this point, the int product2 = number3 * number4 in our Java code is finished.

The remaining blue bytecodes you see correspond to the following line of print statements.

System.out.println ("Value:" + product1 + "," + product2)

From these bytecodes, we can also see that we directly use the plus sign for string concatenation in Java, and the Java compiler automatically uses StringBuilder to optimize when compiling.

Since the product of integer variables needs to be printed, the bytecode iload_2 loads the results of the calculation previously saved in istore_2 in local variable 2 on the stack so that the product results can finally be output.

These are all the contents of the article "what is the difference between integer constants and integer variables in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report