About the i++ and ++i differences between for loops in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.