Java auto-addition and auto-subtraction, java

Source: Internet
Author: User

Java auto-addition and auto-subtraction, java

 1 public class Add { 2  3     public static void main(String[] args) { 4         int i = 0; 5         i=i++ + ++i; 6         int j = 0; 7         j= ++j + j++ + j++ + j++; 8         int k = 0; 9         k=k++ + k++ + k++ + ++k;10         int h = 0;11         h=++h + ++h;12         int p1=0,p2=0;13         int q1=0,q2=0;14         q1=+p1;15         q2=p2++;16         System.out.println("i "+i);17         System.out.println("j "+j);18         System.out.println("k "+k);19         System.out.println("h "+h);20         System.out.println("p1 "+p1);21         System.out.println("p2 "+p2);22         System.out.println("q1 "+q1);23         System.out.println("q2 "+q2);24 25     }26 27 }

Output

i 2j 7k 7h 3p1 0p2 1q1 1q2 0

Resolution: The difference between I ++ and I is that one is the auto-increment after the program is completed, and the other is the auto-increment before the program starts.

The "I = I ++ I" Execution Process is to execute I ++ first, but the I auto-increment 1 operation is executed only later, so I is still 0, then execute ++ I. After ++ I, the value of I is 1. After ++ I is executed, add I ++, so now the I value is actually 2, 0 + 2 = 2, and then assigned to I, the final I value is 2.

"J = ++ j + j ++", the execution process is first ++ j, so the value of j is 1, after j ++ is executed, the value of j after j ++ is still 1, and then j ++ is executed. The execution result is still 1, however, to add the j ++ just now, the actual value of j is 2, and then the last j ++ is executed. The result after execution is still 2, however, we need to add the j ++ just now, so the j value is actually 3, so 1 + 1 + 2 + 3 = 7, and then assign the value to j, the final value of j is 7.

The execution process of "k = k ++" is k ++, so the value of k is 0, then, after k ++ is executed, the value of k after k ++ is still 0, but the value of k must be supplemented with k ++. Therefore, the value of k is actually 1, then execute the last k ++, And the execution result is still 1, but we need to add the k ++ just now, so the value of K is actually 2, the final execution is + + k, and the execution result is 3. Then add k ++. the actual result of k is 4, so 0 + 1 + 2 + 4 = 7, then assign the value to k, and the final value of k is 7.

"H = ++ h" is the first Auto-increment h, the h value is 1, and then the auto-increment is good, the h value is 2, so 1 + 2 = 3, then assign the value to h. The final value of h is 3.

"Q1 = ++ p1" first increments p1. The value of p1 is 1, and then is assigned to q1. Therefore, the value of q1 is 1.

"Q2 = p2 ++" first assigns p2 to q2. The q2 value is 0, and then auto-incrementing p2. Therefore, the p2 value is 1.

Related Article

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.