How do the post increment (i++) and pre increment (++i) operators work in Java?

后端 未结 14 2482
暖寄归人
暖寄归人 2020-11-21 04:45

Can you explain to me the output of this Java code?

int a=5,i;

i=++a + ++a + a++;
i=a++ + ++a + ++a;
a=++a + ++a + a++;

System.out.println(a);
System.out.p         


        
相关标签:
14条回答
  • 2020-11-21 04:51

    Presuming that you meant

    int a=5; int i;
    
    i=++a + ++a + a++;
    
    System.out.println(i);
    
    a=5;
    
    i=a++ + ++a + ++a;
    
    System.out.println(i);
    
    a=5;
    
    a=++a + ++a + a++;
    
    System.out.println(a);
    

    This evaluates to:

    i = (6, a is now 6) + (7, a is now 7) + (7, a is now 8)
    

    so i is 6 + 7 + 7 = 20 and so 20 is printed.

    i = (5, a is now 6) + (7, a is now 7) + (8, a is now 8)
    

    so i is 5 + 7 + 8 = 20 and so 20 is printed again.

    a = (6, a is now 6) + (7, a is now 7) + (7, a is now 8)
    

    and after all of the right hand side is evaluated (including setting a to 8) THEN a is set to 6 + 7 + 7 = 20 and so 20 is printed a final time.

    0 讨论(0)
  • 2020-11-21 04:51

    pre-increment and post increment are equivalent if not in an expression

    int j =0;
    int r=0         
    for(int v = 0; v<10; ++v) { 
              ++r;
              j++;
              System.out.println(j+" "+r);
      }  
     1 1  
     2 2  
     3 3       
     4 4
     5 5
     6 6
     7 7
     8 8
     9 9
    10 10
    
    0 讨论(0)
  • 2020-11-21 04:57

    ++a is prefix increment operator:

    • the result is calculated and stored first,
    • then the variable is used.

    a++ is postfix increment operator:

    • the variable is used first,
    • then the result is calculated and stored.

    Once you remember the rules, EZ for ya to calculate everything!

    0 讨论(0)
  • 2020-11-21 04:58
    i = ++a + ++a + a++;
    

    is

    i = 6 + 7 + 7
    

    Working: increment a to 6 (current value 6) + increment a to 7 (current value 7). Sum is 13 now add it to current value of a (=7) and then increment a to 8. Sum is 20 and value of a after the assignment completes is 8.

    i = a++ + ++a + ++a;
    

    is

    i = 5 + 7 + 8
    

    Working: At the start value of a is 5. Use it in the addition and then increment it to 6 (current value 6). Increment a from current value 6 to 7 to get other operand of +. Sum is 12 and current value of a is 7. Next increment a from 7 to 8 (current value = 8) and add it to previous sum 12 to get 20.

    0 讨论(0)
  • 2020-11-21 04:58

    In the above example

    int a = 5,i;
    
    i=++a + ++a + a++;        //Ans: i = 6 + 7 + 7 = 20 then a = 8 
    
    i=a++ + ++a + ++a;        //Ans: i = 8 + 10 + 11 = 29 then a = 11
    
    a=++a + ++a + a++;        //Ans: a = 12 + 13 + 13 = 38
    
    System.out.println(a);    //Ans: a = 38
    
    System.out.println(i);    //Ans: i = 29
    
    0 讨论(0)
  • 2020-11-21 05:04

    ++a increments and then uses the variable.
    a++ uses and then increments the variable.

    If you have

    a = 1;
    

    and you do

    System.out.println(a++); //You will see 1
    
    //Now a is 2
    
    System.out.println(++a); //You will see 3
    

    codaddict explains your particular snippet.

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