We should all know that the difference between i++ and ++i is:
++i is to perform i = i +1 and then use the value of I, while i++ is first using the value of I again to perform i = i + 1;
The execution order for the For loop is as follows:
for (A;B;C)
{
D
}
Into the loop execution a;//just enter the time to execute
Executive B; The condition is true to execute D, or jump out for the
Execution D;
Implementation C;
Go back to the 2nd step and start the execution.
Consider the following example:
for (int i = 0; I < 10;i++) {
System.out.println (i);
}
Equivalent:
for (int i = 0; i < 10;) {
System.out.println (i);
i++;
}
2.for (int i = 0; i<10; ++i) {
System.out.println (i);
}
Equivalent: for (int i = 0; I < 10;)
{
System.out.println (i);
++i;
}
In the body of the loop, the i++ and the ++i function the same.
The information printed out is:
0
1
2
3
4
5
6
7
8
9
Printing information proves that i++ and ++i have the same effect.
But there must be some difference, so the cycle time is also printed out.
Number of cycles = 100:
i++ Time is: 5
++i Time is: 3
Number of cycles = 1000:
i++ Time is: 25
++i time is: 21
Number of cycles = 10000:
i++ Time is: 179
++i Time is: 130
Yes, that's the difference between running time. The i++ statement in Java requires a temporary variable to fetch the value before the increment, and ++i does not need it. This causes the system to request a memory space first when using i++, then plug the value into it, and then release it at last. This is a series of operating time.
We can chew on the beginning of this sentence: ++i is the first to execute i = i +1 and then use the value of I, and i++ is first using the value of I and then execute i = i + 1;
One is the first execution and then in the use of the value of I, one is the first use of the value of I to execute, the running time can be imagined. This is the blogger's personal experience, the wrong place to pay attention to the discussion thank you.
About the i++ and ++i differences between for loops in Java