preincrement / postincrement in java

后端 未结 5 542
粉色の甜心
粉色の甜心 2020-12-01 17:35

Can someome help me to understand why:

int i=1;
int j=1;
int k=1;
int l=1;

System.out.println(i++ + i++);  
System.out.println(++j + ++j);  
System.out.pri         


        
相关标签:
5条回答
  • 2020-12-01 18:02

    As the name indicates, a post increment increments the value of the variable AFTER the variable has been processed (read) while the pre incrment increments the value BEFORE.

    For i, that means that first i is incremented by 1, but read as 1, then incremented by 1 again (already being 2 now from the first increment), thus incremented to 3, but read as 2. This results in 1+2 = 3 and the value of i will be 3 as well...

    0 讨论(0)
  • 2020-12-01 18:10
    i++ + i++
    

    means use i, then increment, so i is pushed to some kind of stack,

    then increased by 1,

    then the operator (+) is pushed to the stack,

    then i (now 2) is pushed to the stack.

    Since the expresseion is now over, the values and operator are popped: the 2nd i is 2, the first i is 1, 2+1=3 (i is now 3, since it was incremented after being pushed).

    The thing you are probably missing is that i isn't increased after the evaluation of the whole expression, in the case of a postincrement, and vice versa for preincrement.

    0 讨论(0)
  • 2020-12-01 18:19

    Variable++ means: Increment variable AFTER evaluating the expression.

    ++Variable means: Increment variable BEFORE evaluating the expression.

    That means, to translate your example to numbers:

    System.out.println(i++ + i++);  //1 + 2
    System.out.println(++j + ++j);  //2 + 3
    System.out.println(k++ + ++k);  //1 + 3
    System.out.println(++l + l++);  //2 + 2
    

    Does this clear things up, or do you need further explanations?

    To be noted: The value of all those variables after the 'println' equal '3'.

    Since the OP asked, here's a little 'use-case', on where this behaviour is actually useful.

    int i = 0;
    while(++i < 5) {           //Checks 1 < 5, 2 < 5, 3 < 5, 4 < 5, 5 < 5 -> break. Four runs
        System.out.println(i); //Outputs 1, 2, 3, 4 (not 5) 
    }
    

    Compared to:

    int i = 0;
    while(i++ < 5) {           //Checks 0 < 5, 1 < 5, 2 < 5, 3 < 5, 4 < 5, 5 < 5 -> break. Five runs
        System.out.println(i); //Outputs 1, 2, 3, 4, 5
    }
    
    0 讨论(0)
  • 2020-12-01 18:24

    Things to know:
    1. Java evaluates expressions from left to right
    2. ++i-pre-increment i.e increment before assignment
    3. i++ - post increment i.e. increment after assignment

    System.out.println(i++ + i++);
    

    op1=i++
    op2=1++
    sum=op1+op2
    i++ - post increment the value of i

    1. Assign i to op1 and then increment the value of i.op1=1,i=2
    2. Assign i to op2 and then increment the value of i.op2=2,i=3
    3. Sum=3

      System.out.println(++j + ++j);

    op1=++j
    op2=++j
    sum=op1+op2
    ++i - Pre increment the value of i

    1. now at first j will be incremented to 2 and then assigned to op1.
    2. Then, j will again be incremented to 3 and assigned to op2
    3. Then, op1 and op2 will be added to print the sum as 5 and the value of j will be

      System.out.println(k++ + ++k);

    op1=k++
    op2=++k
    sum=op1+op2

    1. Assign the value of k to op1 and then increment k. op1=1,k=2

    2. Increment the value of k and then assign to op2. op2=3,k=3

    3. Sum = 4

      System.out.println(++l + l++);

    Apply the above logic here also.

    0 讨论(0)
  • 2020-12-01 18:26

    System.out.println(i++)

    -> produces 1 since it increments after printing, but when call it twice u will have 1 + 2 so u can translate it to

    System.out.println(int i=i+1, plus int i = i + 1 -> this gives 2)

    fist phrase gives 2 and second gives 3 but u print it before they i++ increments , so u will have 1 + 2 at the end

    System.out.println(++j);

    -> produces 2 since it increments before printing

    So when u will have ++j = 2 and then ++j = 3 so ++j and ++j is now 5

    0 讨论(0)
提交回复
热议问题