Understanding of I ++ problems in java

Source: Internet
Author: User

Understanding of I ++ problems in java

Understanding of I ++ problems in java

Interview question analysis

During the last Tuesday interview, I encountered a questionable pen question. Here I will analyze it in detail to find out the cause of the problem, solve the confusion, and avoid the same problem again.

The following code is used: The for loop traverses 20 times and copies the count ++ value to count. What is the result of count?

public static void main(String[] args) throws Exception {        int count = 0;        for (int i = 0; i < 10; i++) {            count = count++;            // count= ++count;            // count=count++ + ++count;            // count=++count + count++ ;            System.out.println(count== + count);        }        System.out.println(count);    }
Difficult questions

This topic mainly describes how count = count ++ is executed in the Java Virtual Machine in sequence.

In java, count = count ++ stores the count value and then executes count ++, then copy the previously stored value to count (instead of copying the value after count + 1 to count). What we see is that no matter how values are assigned, the count value is always the original default value. In C language, I guess the answer will be different. According to a student, the answer is different. I didn't test it.

Update: in (21:04:55) for further understanding, refer to the content in the interview book, count = count ++, which belongs to java using the intermediate variable caching mechanism, let's use a complex example to understand it. In this case, the Execution Code is as follows: count = 1, count = count + count ++; Specific write: x = (count + count ++ ), y = (x + count ++), z = (y + count ++); count = z, let's take a look at x = (count + count ++), count = 1, and x = count + count = 2. Adding 1 to count is equal to 2; y = (x + count ++), y = x + count = 2 + 2 = 4, count Add 1 equals 3, z = (y + count ++ ), z = y + count = 4 + 3 = 7, count Add 1 equals 4

Extended content

When we change count = count ++ to count = + + count, what is the content and the result is the so-called 1, 2, 3, 4... .

The execution process is: first count and Add 1, and then assign the result of auto-increment to count, so it will not remain unchanged.

Next, let's discuss how much the result will be if we replace count = count ++ with count = count ++ count.

In this case, the execution process of count is: first execute the rightmost count + 1, we assume that the initial value of count is 0, then after the rightmost auto-increment of 1, the result becomes, count = 1, and then execute count = count + count. At this time, the count content has already changed from 0 to 1, + The count value before and after the number is 1, so that the result of adding the two parts is 2, and the first result is indeed 2.

When we replace count = count ++ with count = ++ count + count ++, what is the result?

The result is the same as the preceding one. The first result is still 2. the execution order is: Execute the ++ count next to the given = sign first, that is, let count Add 1, so that the count content is changed from 0 to 1, and then the two 1 are added, which is the so-called 2.

The above is what I want to say, count = count ++; some people may wonder why the count suffix plus 1 is not assigned to count, but the prefix plus 1, after modifying the count content, you can assign values to count. In fact, as mentioned in the bold text above, the memory will be temporarily stored in the previous, in count = count ++, after jvm analysis, the count suffix is added with 1, records the original count value, stores it in a bucket, assigns it to count, and Adds 1 to count. The prefix and 1 have the highest priority. At the beginning, the count content, jvm syntax analysis, and semantic analysis are directly modified, and the count content is assigned to count. Through such a test, we can roughly describe this process. After all, the comparison of the final test results and multiple groups of data also proves my conjecture. Of course, it is more in-depth, let's take a look at how the jvm analyzes and processes it.

Add another question

The following is a question similar to the above:

public class MyTest {    static{        int x=5;    }    static int x,y;    public static void main(String[] args) {        x--;        myMethod();        System.out.println(x+ y++ + x);    }    private static void myMethod() {        // TODO Auto-generated method stub        y= x++ + ++x;    }}

The result is 2. The reason is analyzed by yourself.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.