Home >Java >javaTutorial >Detailed introduction to the java self-increment and self-decrement operation process

Detailed introduction to the java self-increment and self-decrement operation process

黄舟
黄舟Original
2017-03-21 10:58:351666browse

This article mainly introduces the java self-increment and self-decrement operation process. It is very good and has reference value. Friends who need it can refer to it.

No more nonsense, I will directly post the code for everyone. , the specific code is as follows:

public class Add {
  public static void main(String[] args) {
    int i = 0;
    i=i++ + ++i;
    int j = 0;
    j= ++j + j++ + j++ + j++;
    int k = 0;
    k=k++ + k++ + k++ + ++k;
    int h = 0;
    h=++h + ++h;
    int p1=0,p2=0;
    int q1=0,q2=0;
    q1=+p1;
    q2=p2++;
    System.out.println("i "+i);
    System.out.println("j "+j);
    System.out.println("k "+k);
    System.out.println("h "+h);
    System.out.println("p1 "+p1);
    System.out.println("p2 "+p2);
    System.out.println("q1 "+q1);
    System.out.println("q2 "+q2);
  }
}

Output

i 2
j 7
k 7
h 3
p1 0
p2 1
q1 1
q2 0

Analysis: The difference between i++ and ++i is that one is the auto-increment after the program is completed, and the other is the auto-increment before the program starts. increase.

The execution process of "i=i++ + ++i" is to execute i++ first, but the operation of incrementing i by 1 is executed later, so i is still 0 at this time, and then ++i,++ is executed. The value of i after i is 1. After executing ++i, i++ must be added, so the value of i at this time is actually 2,0+2=2, and then assigned to i, the final value of i is 2.

"j= ++j + j++ + j++ + j++", the execution process is to first ++j, so the value of j is 1, and then execute j++. After j++, the value of j is still 1, and then execute j++, the result after execution is still 1, but the j++ just now needs to be supplemented, so the actual value of j at this time is 2, and then the last j++ is executed, the result after execution is still 2, but the j++ just now needs to be supplemented, So the value of j at this time is actually 3, so 1+1+2+3=7, and then assigned to j, the final value of j is 7.

“k=k++ + k++ + k++ + + +k" is executed by k++ first, so the value of k is 0, and then k++ is executed. After k++, the value of k is still 0, but the previous k++ needs to be supplemented, so the value of k is actually 1 at this time, and then Execute the last k++ again, and the result after execution is still 1, but the previous k++ needs to be supplemented, so the value of K is actually 2 at this time. Finally, ++k is executed, and the execution result is 3. Then the previous k++ is supplemented. , the actual result of k is 4, so 0+1+2+4=7, and then assigned to k, the final value of k is 7.

"h=++h + ++h" is to increment h first, and the value of h is 1, and then increment it by itself. The value of h is 2, so 1+2=3, and then assign it to h. The final value of h is 3.

"q1=++p1" first increments p1, the value of p1 is 1, and then assigns it to q1, so the value of q1 is 1.

" q2=p2++" first assign p2 to q2, the value of q2 is 0, and then increment p2, so the value of p2 is 1.

The above is the detailed content of Detailed introduction to the java self-increment and self-decrement operation process. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn