Increment operator + +, increase the value by 1, the decrement operator---to reduce the value by 1
Operator |
Code Snippets |
Difference |
++ |
x = 2 * m + +; |
First Run x = 2 * m; Re-run m = m + 1; |
++ |
x = 2 * ++m |
First run m = m + 1; Run x = 2 * m again; |
-- |
y = 2 * m-- |
Run y = 2 * m first; Re-run m = m-1; |
-- |
y = 2 *--m |
First run m = m-1; Run y = 2 * m again; |
Self-increment code:
Package Com.scd.chapter1;public class Test { /** * @param args * /public static void main (string[] args) { //TODO auto-generated method stub int m = 7; int n = 7; int x = 2 * m++; int y = 2 * ++n; System.out.println ("m=" +m); System.out.println ("n=" +n); System.out.println ("x=" +x); System.out.println ("y=" +y);}
Output Result:
M=8n=8x=14y=16
Self-Reducing code:
Package Com.scd.chapter1;public class Test { /** * @param args * /public static void main (string[] args) { //TODO auto-generated method stub int m = 7; int n = 7; int x = 2 * m--; int y = 2 *--n; System.out.println ("m=" +m); System.out.println ("n=" +n); System.out.println ("x=" +x); System.out.println ("y=" +y);}
Output Result:
M=6n=6x=14y=12
Java Basics (i) Self-increment, decrement operator