Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

Is there any difference between x++ and ++x in Java?

1 Answer

0 votes
by (13.1k points)

We call x++ as postincrement and ++x as preincrement.

In x++ the variable value is used first in the expression and then it is incremented after the operation.

In ++x the variable is incremented and then uses the new value in the expression. 

Here is an example that will explain about them:

int x = 5, y = 5;

System.out.println(++x); // outputs 6

System.out.println(x); // outputs 6

System.out.println(y++); // outputs 5

System.out.println(y); // outputs 6

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...