Is there a difference between ++x and x++ in java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Yes, using ++X, X+1 will be used in the expression. Using X++, X will be used in the expression and X will only be increased after the expression has been evaluated.
So if X = 9, using ++X, the value 10 will be used, else, the value 9.
If it's like many other languages you may want to have a simple try:
If the above doesn't happen like that, they may be equivalent
There is a huge difference.
As most of the answers have already pointed out the theory, I would like to point out an easy example:
Now let's see
++x
:The Question is already answered, but allow me to add from my side too.
First of all ++ means increment by one and -- means decrement by one.
Now x++ means Increment x after this line and ++x means Increment x before this line.
Check this Example
It will give the following output:
++x is called preincrement while x++ is called postincrement.
In Java there is a difference between x++ and ++x
++x is a prefix form: It increments the variables expression then uses the new value in the expression.
For example if used in code:
x++ is a postfix form: The variables value is first used in the expression and then it is incremented after the operation.
For example if used in code:
Hope this is clear. Running and playing with the above code should help your understanding.