Differences in the self-increment in Java self-increment (+ +) and C languages

Source: Internet
Author: User

in the In high-level languages such as Java and C, the function of self-increment and decrement is basically the same, and the variable itself is added one or minus one. The following is a description of the self-increment, which is similar to the self-reduction.

The self-increment operator (+ +) has two forms of writing, one before the variable: + + num;

The other is num++ after the variable ; they work the same, but the priorities vary greatly.

Note: The priority represents the order in which the operator executes, and the higher the precedence, the first execution. Baidu Encyclopedia priority table is as follows:

Code Demo:

Public class Test {

Public Static void Main (string[] args) {

int a = 1;

int b = 0;

b = a++;// equivalent to b=a; a++;

System. out. println ("a=" + A + ", b=" + b);

System. out. println ("===== Gorgeous split line =====");

A = 1;//re-assigned to a value of 1

b = 0;//re-assigned to a value of 0

b = ++a;// equivalent to a++;b = A;

System. out. println ("a=" + A + ", b=" + b);

}

}

Output Result:

A=2,b=1

===== Gorgeous split-line =====

a=2,b=2

pre-Gaga priority high first operation, after + + priority low after operation, in C language here is the same way of operation.

===========================================================

differences:

Java code:

Public class Test {

Public Static void Main (string[] args) {

int a = 1;

A = a++;

System. out. println (a);// output result is 1

}

}

C Language code:

#include <stdio.h>

void Main () {

int a = 1;

A = a++;

printf ("%d", a);// output result is 2

}

===============================================================

Conclusion:in theIn Java, we can explain, after the addition of a first to add a, and then the value of no operation is assigned to a, the final result a==1.in theC language is in accordance with the priority of the operation, after a low priority, a first assignment to a itself, and then a again Gaga, the final result a==2;

Differences in the self-increment in Java self-increment (+ +) and C languages

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.