Junior — Middle
Can you explain what the operation of decreasing a variable's value by one means?
sobes.tech AI
Answer from AI
Decrementing a variable by one in Java is usually done using the decrement operator --. It decreases the current value of the variable by 1.
There are two forms:
- Postfix decrement:
i--— returns the current value first, then decreases. - Prefix decrement:
--i— decreases the value first, then returns.
Example:
int i = 5;
System.out.println(i--); // Outputs 5, then i becomes 4
System.out.println(--i); // First i becomes 3, then outputs 3