June 2018
Beginner
722 pages
18h 47m
English
The following code shows how the ++ and -- operators work, depending on their position, before the variable (prefix) or after the variable (postfix):
int i = 2;System.out.println(++i); //prints: 3System.out.println("i=" + i); //prints: i=3System.out.println(--i); //prints: 2System.out.println("i=" + i); //prints: i=2System.out.println(i++); //prints: 2System.out.println("i=" + i); //prints: i=3System.out.println(i--); //prints: 3System.out.println("i=" + i); //prints: i=2
If placed as a prefix, it changes its value by 1 before the variable's value is returned. But when placed as a postfix, it changes its value by 1 after the variable's value is returned.
Read now
Unlock full access