Java Incremental operator query (++i and i++)
This question already has an answer here:
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 be incremented next. That means you will get the present value of x
for the operation and if you use x next time will get the incremented value;
In case of ++x
:
First the current value will be incremented and it will be used (the incremented value) next, that means you will get the incremented value at this operation and for other after this operation.
Now lets split the code and discuss them separately
method: sample1() :
private static int sample1(int i) {
return i++;
}
This method will take a int and return it first and then try to increment but as after returning the variable i
will go out of 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 will take a int and increment it first and then return it. exp in: 10-> out 11
In both case only the variables will change locally, that means if you call from main method the variables of main method will remain unaffected by the change (as the sample1() and sample2() are making copy of the variables)
Now for the code of the main method
System.out.println(sample1(i++)); // it's giving sample1() `i=0` then making `i=1`
// so sample1() will return 0 too;
System.out.println(sample1(++i)); // it's making `i=2` and then giving sample1() `i=2`
// so sample1() will return 2;
System.out.println(sample2(j++)); // it's giving sample2() `j=0` then making `j=1`
// so sample2() will return 1;
System.out.println(sample2(++j)); // it's making `j=2` giving sample2() `j=2` then
// so sample2() will return 3;
Since sample1
and sample2
are just modifying their own local variables i
and j
(not those of the calling method), it's clearer if we rewrite them without those modifications:
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 which point it's straightforward to just substitute 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 separating the incrementations into their own commands. i++
evaluates to the original value of i
, so it's like incrementing i
after running the surrounding command; ++i
, by contrast, is like incrementing i
before running the surrounding 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 which point it should be straightforward to trace through and see what it will output.
Does that all make sense?
You're experiencing the fun of prefix and postfix operators.
The prefix operator, ++i
increments the variable i
by one before using it in the expression, where the postfix operator ( i++
) uses i
in the expression before incrementing it.
This means that your method sample1
doesn't do anything; it evaluates the expression containing i
, but because that expression is a return statement, the local variable i
goes out of scope, and we can't modify it anymore.
sample2
, by contrast, increments the local copy of j
before returning it, which is why you print higher values of j
than you expect.
上一篇: java的