Post and Pre increment statements

When I run the following example, I get output 0,2,1

class ZiggyTest2{ static int f1(int i) { System.out.print(i + ","); return 0; } public static void main(String[] args) { int i = 0; int j = 0; j = i++; //After this statement j=0 i=1 j = j + f1(j); //After this statement j=0 i=1 i = i++ + f1(i); //i++ means i is now 2. The call f1(2) prints 2 but returns 0 so i=2 and j=0 System.out.println(i); //prints 2? } } 

I do not understand why the output is 0.2.1 and not 0.2.2

+8
java scjp ocpjp
source share
7 answers
  i = i++ + f1(i); 

i++ means i now 2 . The call f1(i) prints 2 , but returns 0 so i=2 and j=0

before that i = 1 , now imagine f1() called and replaced by 0

So

 i = i++ + 0; 

now it will be

 i = 1 + 0 // then it will increment i to 2 and then (1 +0) would be assigned back to `i` 

In simpler words (from @Piotr here )

"i = i ++" roughly translates to

 int oldValue = i; i = i + 1; i = oldValue; 

Another such example:

+3
source share

If we expand the operator i = i++ + f1(i) , we get something like the following

 save the value of i in a temp variable, say temp1 // temp1 is 1 increment i by one (i++) // i gets the value 2 execute f1(i), save return value in, say temp2 // temp2 is 0, print 2 assign temp1 + temp2 to i // i becomes 1 again 

I think that the main steps can be summarized as described above.

+5
source share

The solution can be understood from this example.

 public static void main(String[] args) { int i = 0; i = i++; System.out.println("i is" + i); } /* The output is "i is 0" */ 

Therefore, from this line

 i = i++ + f1(i); 

Your self is still 1, and, obviously, the function will return 0. This is again stored in self and, therefore, the value 1. Instead of the updated value of i stored in i, you override it with the assignment operator.

+1
source share

Pre increment means: adds one to the variable and returns the incremented value; Post increment - first return i, and then increase it;

 int i, j, k; i = 0; // 0 j = i++; // return i , then increment i // j = 0; i = 1; k = ++i; // first increment and return i //k = 2; i = 2; // now ++j == --k == --i // would be true => 1==1==1; // but , using post increment would // j++ == k-- == i-- // false because => 0 == 2 == 2; // but after that statement j will be 1, k = 1, i = 1; 
+1
source share

Hope this explanation may be helpful:

 j = i++; // Here since i is post incremented, so first i being 0 is assigned to j // and after that assignment , i is incremented by 1 so i = 1 and j = 0. i = i++ + f1(i); // here again since i is post incremented, it means, the initial value // of i ie 1 as in step shown above is used first to solve the // expression i = i(which is 1) + f1(1)**Since i is 1** // after this step the value of i is incremented. so i now becomes 2 // which gets displayed in your last System.out.println(i) statement. 

try it

 i = ++i + f1(i); // here i will be first inremented and then that value will be used // to solve the expression i = i + f1(i)' 

So, a short time in incremental increment expression is first resolved, and then the value increases. But in pre increment, the value first increases, and then the expression is resolved.

But if you write only

 i++; ++i; 

Then both mean the same thing.

Hi

+1
source share

In the field "Increment operator" the value of the operand will increase after use. Example

 int k =1; int l = k++; System.out.println("...k..."+k+"...l.."+l); 

First, k (value = 1) is assigned l, after which the value of k will increase

The same thing happens on the next line

 i = i++ + f1(i); 
+1
source share

To get in-depth analysis, you need to see Expression and its evaluation order.

Here's a little explanation regarding the score i ++ + f1 (i)

In the Equation Compiler, get an ā€œiā€ that is 1 , and put it on the stack as the first operand , then increments ā€œiā€ , so its value will be 2 and it will calculate the second operand, calling a function that will be 0 , and at that time Operations ( +) will be 1 and 0 .

+1
source share

All Articles