i = i++ in Java

Source: Internet
Author: User

Reprint: http://soft.chinabyte.com/database/279/11431779.shtml

Keyword: Java

A person who has studied a computer language must know the result of the j=i++ operation, the value of the whole expression is I, and the value of I becomes i+1; So what's the situation with i=i++? Here's a test in both Java and C:

Java:

public class Test

{

public static void Main (String args[])

{

Inti,j;

i=0;

for (j=0;j<5;j++)

{

i=i++;

System.out.println ("i=" +i);

}

}

}

C:

#include

Main ()

{

Inti,j;

i=0;

for (j=0;j<5;j++)

{

i=i++;

printf ("i=%d\n", I);

}

}

You may ask, what is the problem with such a simple program? Yes, if you don't believe it, you can run the two programs and the results are very shocking.

The results from the first Java program are as follows:

I=0

I=0

I=0

I=0

I=0

The result of the second C program is as follows:

I=1

i=2

I=3

I=4

I=5

The same is i=i++, why the result will be so big difference? The original is a problem on the compiler, the Java compiler in the encounter i++ and I will be re-allocated a piece of memory space for the variable operation, to hold the original value, and after the completion of the assignment operation, the memory is released, the following first look at if the case is j=i++:

The original value of i is stored in the post-open memory, finally this value will be assigned to J, so that after j=i++, J will get the value of I, and I will be added, so, after releasing the memory, the original place where J and I will get the value will be: J (at this time the value is equal to the initial I value) and I (I self-added value).

Understanding the above question, let's take a look at the situation of i=i++:

So the result in this last loop is still I (i.e. 0).

The i=i++ in the C language is just the completion of i++ content, so the conclusion will be different. This situation shows that Java and C processing syntax of different mechanisms, if in the program only input i++ will not appear in this aspect of the problem, so everyone in the future program if you use the i=i++ to be extra careful, generally only need to use i++ there will be no problem.

i = i++ 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.