[Photo + Video] novice-friendly Java ++ i (before increment) and i ++ (the increment) Detailed usage

Obviously the book written ++ i (before increment) and i ++ (the increment) role is not the same.

But it seems that their role is as it seems in my eyes? ?

The problem how to break? Come watch this issue [Photo + Video] it.

Allows you to quickly grasp ++ i (before increment) and i ++ (the increment).

Video Tutorial:

Click here to watch the B station

Knowledge points to explain:

  • ++ i before the increment:
  1. In using an i of before the i value is incremented by 1 ,
  2. Thus executing the ++ i , the entire expression and i values are i + 1
  • after i ++ increment:
  1. In using an i of after the i value is incremented by 1 ,
  2. Thus after executing i ++ , the entire expression value i
  3. And the value of i becomes i + 1
  • ||: Logical OR:
  1. As long as there is a condition result is true that is true
  2. All conditions results are only false, false

Example 1 analysis: (i and j increment after both)

package com.task03;

public class P044_ {

    public static void main(String[] args) {
        int i = 0, j = 1;
        if ((i++ == 1) || (j ++== 2)) {//此时i++==0   j++==1
            j = 42;
        }
        System.out.println("i=" + i + ",j=" + j);//i=1,j=2
    }
}

1. Initialize i = 0 j = 1

Due to the use after the increment i, so that the value of i by 1

After performing i ++, whole expression is i, and the value of i becomes i + 1

2. Therefore, after performing i ++, i is the value of the overall expression does not satisfy the conditions 0 to i ++ == 1

Likewise after performing j ++, j is the value of the entire expression 1 does not satisfy the condition j ++ == 2

3 can not be performed j = 42;

4. Because after performing i ++, the value of i becomes i + 1, j becomes the value of j + 1

Therefore, after performing the output statement i = 1, j = 2

Example 1 Pictures: (i and j increment after both)

Here Insert Picture Description

Analysis example 2: (after i is incremented, j is incremented before)

package com.task03;

public class P044_ {

    public static void main(String[] args) {
        int i = 0, j = 1;
        if ((i++ == 1) || (++j == 2)) {//此时i++==0   ++j==2
            j = 42;
        }
        System.out.println("i=" + i + ",j=" + j);//i=1,j=42
    }
}

1. Initialize i = 0 j = 1

Since self-energizing before: In using j of the front , so that j value plus 1 ,

Thus executing the j ++ , the entire expression and j values are i + 1

2. Therefore, after performing i ++, i is the value of the overall expression does not satisfy the conditions 0 to i ++ == 1

After the execution ++ j, the entire expression is the value of j + 1 2 ++ j == satisfies Condition 2

3. Perform j = 42;

4. Because after performing i ++, the value of i becomes i + 1, j becomes 42

Therefore, after performing the output statement i = 1, j = 42

Picture 2 examples: (i after increment, j for the first increment)

Here Insert Picture Description

Pre-decrement (--i) and post-decrement (i--) is the same, we can use the example code here, to understand what their own digestion. This effect will be better, memories will be more profound! Look forward to your homework in the comments section oh ~

We recommend that you look at:

If you find this article pretty good, praise the trouble to help me point a duck! So that more people can see this article. I have more motivation to continue technical documentation -
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/Qpgshare/p/12484503.html