The code
result++; and ++result; will both end in result being incremented by one. The only difference is thatthe prefix version (
++result) evaluates to the incremented value, whereas the postfix version (
result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose.
But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.
The following program, PrePostDemo, illustrates the prefix/postfix unary increment operator:
class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
System.out.println(i); // "4"
++i;
System.out.println(i); // "5"
System.out.println(++i); // "6"
System.out.println(i++); // "6"
System.out.println(i); // "7"
}
}
Reference
0 comments:
Post a Comment