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 is the difference between a=a+1 and astat1 in java

2025-04-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what is the difference between a=a+1 and aversion 1 in java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The difference between java a=a+1 and astatine 1

The method called by the test case public static void test (Object obj) {System.out.println (obj.getClass ());}

(1) numerical operations with precision less than int (or long) are automatically converted to int (or long), and the result is

Char I ='a candidate Byte j = 2 class java.lang.Integerbyte test (I + j); / / class java.lang.Integer i1 = 1 X short j 1 = 2 x x test (i1 + J1); / /

(2) when the shaping numbers such as char, byte, short, int, long and float (or double) are calculated, the float (double) will be raised first, and the calculation will be carried out.

Int i2 = 1TX float j2 = 1.5f TX test (i2 + J2); / / class java.lang.Float

(3) when float operates with double, float will be promoted to double first.

Float i3 = 1.2f class java.lang.Double double j3 = 1bot test (i3 + j3); / /

(4) the difference between a=a+1 and astatine 1.

Java anecdotes adept + and aura fun a

If you ask the difference between averse + and + + a, I guess many of them can answer it. A value + is to take the value of an and then increase itself. On the contrary, + an increases itself first and then takes the value of a. There are a little bit of programming basics that can be understood without too much explanation, but is it really that simple in Java?

Int a = 1tera = astatine system. Out.println (a)

With what you have learned, can you give the answer without a test?

When I first saw this question, I took it for granted that the answer was: 2. But the cruel truth tells me that the answer is: 1. Faced with this result at that time, I couldn't accept it. I searched the Internet frantically for the explanation of this answer. Limited to the level of knowledge at that time, only this conclusion was remembered, and none of the instructions for deducing this conclusion could be understood. Today, when I read a book and encounter data operation, I think of this problem again, and I rededuce this process according to my own level of knowledge.

First open the generated class file with IDEA.

It intuitively tells us that the result is 1, but this code only explains result, not why. Continue to use the command javap-c, as shown in the figure.

At the level of meeting this problem for the first time, I can barely understand the Java code of the first step, but I can't understand the instructions of the second step completely. Now I can analyze the results from these instructions (here I need to analyze the first five lines of commands, followed by printouts).

Iconst_1

Press the constant 1 into the Operand stack. This is done when the int value is 1-5. If it is-128 to 127, the range is bipush x (x is the actual value). The range of-2 ^ 15 ~ 2 ^ 15-1 is the command sipush x (ditto). The range of-2 ^ 31 ~ 2 ^ 31-1 is the ldc command used.

Istore_1

Pops up the number at the top of the Operand stack (1 in this case) and assigns a value to the first element of the local variable, the a with index 1. The local variable is in the form of an array, and the parameter args with an index of 0 is the parameter main ().

Iload_1

Press the variable with the local variable index 1 (in this case a) into the Operand stack, where the top element of the stack is 1.

Incc 1pr 1 (key)

Add 1 to the number of local variables indexed to 1, and the value of an is 2. Note: this result is not pushed into the Operand stack.

Istore_1

The element at the top of the pop-up stack (1 in this case) is assigned to a, overriding the result where an is 2.

This is about the whole process. Let's explain it with a picture.

This problem is solved, so the following code is easy to understand.

Int a = 1 a = + + a TX system. Out.println (a)

Just take the screenshot.

This is the end of the content of "what is the difference between a=a+1 and aversion 1 in java". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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