Decrement operator | Sololearn: Learn to code for FREE!

+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?

2/15/2017 6:03:00 PM

Efraim Ié

1 Answer

New 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