Java Incremental Operator Query (++ Me and Me ++)

I have the following code:

public class Book { private static int sample1(int i) { return i++; } private static int sample2(int j) { return ++j; } public static void main(String[] arguments){ int i = 0; int j = 0; System.out.println(sample1(i++)); //0 System.out.println(sample1(++i)); //1 System.out.println(sample2(j++));//1 System.out.println(sample2(++j));//2 System.out.println(i);//2 System.out.println(j);//2 } } 

My expected result in the comments. Actual output below:

 0 2 1 3 2 2 

I got confused with function calls and using operator. Can someone kindly explain the actual result?

+6
source share
7 answers

First of all, you need to know the difference between x++ and ++X ;

In case of x++ :

First, the current value will be used, and it will increase as follows. This means that you will get the current x value for the operation, and if you use x the next time it will get an incremental value;

In case of ++X :

First, the current value will be increased, and it will be used (additional value) further, which means that you will receive an increased value during this operation and for others after this operation.

Now let's split the code and discuss it separately

method: sample1 ():

 private static int sample1(int i) { return i++; } 

This method will accept an int and return it first, and then try to increment, but after returning the variable i will disappear from the scope, so it will never be incremented at all. exp in: 10-> out 10

method: sample2 ():

 private static int sample2(int j) { return ++j; } 

This method takes an int and increments it first and then returns. exp in: 10-> out 11

In both cases, only the variables will change locally, which means that if you call from the main method, the variables of the main method will remain unaffected by the change (since sample1 () and sample2 () copy the variables)

Now for the main method code

 System.out.println(sample1(i++)); // it giving sample1() `i=0` then making `i=1` // so sample1() will return 0 too; System.out.println(sample1(++i)); // it making `i=2` and then giving sample1() `i=2` // so sample1() will return 2; System.out.println(sample2(j++)); // it giving sample2() `j=0` then making `j=1` // so sample2() will return 1; System.out.println(sample2(++j)); // it making `j=2` giving sample2() `j=2` then // so sample2() will return 3; 
+11
source

Since sample1 and sample2 just change their local variables i and j (and not those related to the calling method), the more clear if we rewrite them without these changes:

 private static int sample1(int i) { return i; // was 'i++', which evaluates to the old i } private static int sample2(int j) { return j + 1; // was '++j', which evaluates to j after incrementing } 

At what point is it easy to replace them in place β€” sample1(...) becomes ... and sample2(...) becomes ... + 1 :

 int i = 0; int j = 0; System.out.println(i++); System.out.println(++i); System.out.println((j++) + 1); System.out.println((++j) + 1); System.out.println(i); System.out.println(j); 

We can make this a bit clearer by dividing the increments into our own teams. i++ evaluates the initial value of i , so he likes the increment of i after running the district command; ++i , by contrast, is like incrementing i before running a district command. So we get:

 int i = 0; int j = 0; System.out.println(i); i++; ++i; System.out.println(i); System.out.println(j + 1); j++; ++j; System.out.println(j + 1); System.out.println(i); System.out.println(j); 

., at this moment it should be simple to trace and see what it will output.

Does all this make sense?
+15
source

You enjoy the prefix and postfix operators.

The prefix operator ++i increments the value of the variable i by one before using it in the expression, where the postfix operator ( i++ ) uses the expression i in the expression before incrementing it.

This means that your sample1 method sample1 nothing; it evaluates an expression containing i , but since this expression is a return statement, the local variable i is out of scope, and we can no longer modify it.

sample2 , by contrast, increments the local copy of j before returning it, so you print higher j values ​​than you expect.

+8
source

Easy:
1) First call:
a) Provide me (== 0) in sample1 (), which returns 0 (then it increments the argument and is discarded).
b) i increases due to i ++. I am now 1 .
c) Prints the result of the function: 0.

2) Second call:
a) I increments due to ++ i. I am now 2 .
b) Provide me (== 2) sample1 (), which returns 2 (then it increments the argument i and is discarded)
c) Prints out the result of the function: 2.

3) Third call:
a) Provide j (== 0) sample2 (), which increments the argument and therefore returns 1.
b) increments j due to j ++. j is now 1 .
c) Prints the result of the function: 1.

4) The fourth call:
a) Increments j due to ++ j. j is now 2 .
b) Provide j (== 2) sample2 (), which increments the argument and therefore returns 3.
c) Prints out the result of the function: 3.

5 and 6) The fifth and sixth call:
a) Prints the value of j: 2.

The key to remembering here is that I ++ increment this variable after passing it as an argument, while ++ I increment this variable before passing it as an argument.

Hope this helps

+2
source

1st print

before the call: i = 0

increment after call

sample1 is called with a value of 0

sample 1 returns 0, the increment is discarded

after the call: i = 1

2nd print

before the call: i = 1

increment before calling

sample1 is called with a value of 2

sample1 returns 2, the increment is discarded

after the call: i = 2

Third seal

before calling: j = 0

increment after call

sample2 is called with a value of 0

sample2 in increments of 0 to 1, returns it

1 is printed

increment j to 1

after the call: j = 1

4th seal

before calling: j = 1

increment before calling

increment j to 2

sample2 is called with a value of 2

sample2 increments from 2 to 3, returns it

3 printed

after the call: j = 2

Fifth fingerprint

prints i

2 printed

6th fingerprint

prints j

2 printed

+2
source

Both of them increase the variable i by one, as i = i + 1;

The difference is that :

++ I first increment the value and then return it

i ++ first returns a value and then increments it

This difference in behavior does not matter in the for loop.

If you want to know the difference, try the following:

 int x = 0; int y = x++; int x = 0; int y = ++x; 

Here x++ returns a value and then increments it, but ++x first increments a value, then returns that value

+2
source

Create your own example,

  private static int sample1(int i) { return i++; } private static int sample2(int j) { return ++j; } public static void main(String[] arguments) { int i = 0; int j = 0; System.out.println(sample1(i++)); //0 System.out.println(sample1(++i)); //1 System.out.println(sample2(j++));//1 System.out.println(sample2(++j));//2 System.out.println(j);//2 System.out.println(j);//2 } 
  • i = 0; sample1 (i ++) -> it passes '0' -> to sample1 return i ++ so, 0 (++) Here it returns 0, but increases by 1, so println = 0, but finally I accept 1
  • i = 1; sample1 (++ i) β†’ it passes '2' β†’ to sample1 return i ++ so, 2 (++) Here it returns 2, so println = 2
  • j is 0; sample2 (j ++) β†’ it passes '0' β†’ to sample2 return ++ j so, (++) 0 Here it returns 1, therefore println = 1.
  • j is 1; sample2 (++ j) β†’ it pass ++ 1 => 2, in sample2 return ++ j so, (++) 2 Here it returns 3, so println = 3. But the gain ends in sample2, and not in main, therefore j still has 2.
  • j = 2
  • j = 2
+2
source

Source: https://habr.com/ru/post/971363/


All Articles