Cheap VPS host selection
Provide server host evaluation information

What is the difference between a++and++a in java

a++ and ++a Both are increment operators, which are used to increase the value of a variable by 1. Their difference lies in their operation order and return value.

  • a++ It is a post increment operator. It returns the value of the variable first, and then increases the value by 1.
  • ++a It is the operator that increases the value first by 1, and then returns the increased value.

The following is sample code to compare their differences:

 int a =  five ;
 int b = a ++ ;   //B=5, a=6 (return the value of a and then add a to 1)
 int c =  five ;
 int d =  ++ c ;   //D=6, c=6 (first add 1 to c, then return the value of c)

Summary: Use a++ First return to the original value and then auto increment; use ++a When the value is zero, it will automatically increase and then return the increased value.

Do not reprint without permission: Cheap VPS evaluation » What is the difference between a++and++a in java