Decrement operator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Decrement operator

public class Decrement { public static void main(String[] args) { int n = 7; n++; System.out.println("n == " + n); n--; System.out.println("n == " + n); } } n == 8 n == 7 I'm confused. To decrement he use my previous value? Or is another rule?

15th Feb 2017, 6:03 PM
Efraim Ié
Efraim Ié - avatar
1 Answer
+ 5
In your code, n is initialized with a value of 7. You increment n to 8 and output its value. Then you decrement n (restated, n = 8 - 1) to 7 and output its value again. n++ is the same as n = n + 1 n-- is the same as n = n - 1
15th Feb 2017, 6:09 PM
Ben Harris
Ben Harris - avatar