Java Increment Operator
Java Increment Operator ?
Increment operators are used to increase the value of the variable by one. The increment operator ++ adds 1 to its operand,
Java Increment Operator Syntax:
++
// increment operator
➦Both increment and decrement operator are used on single operand or variable, so it is called as unary operator.
➦ Unary operators are having higher priority than the other operators it means unary operators are execute before other operators
Increment and Decrement Operators
Operator | Description | Example |
---|---|---|
++ | Post Increment | a++ |
-- | Post Decrement | a-- |
++a | Pre Increment | ++a |
--a | Pre Decrement | --b |
Java Increment Operator: Example 1
Output :
11
Java Increment Operator: Example 2
Output :
Before x++: Value of x = 10 y = 20 After x++: Value of x = 11
Java Decrement Operator ?
Decrement operators are used to decrement the value of the variable by one. The decrement operator -- subtracts 1 to its operand,
Java Decrement Operator Syntax:
--
// decrement operator
Java Decrement Operator: Example 1
Output :
Before x--: Value of x = 10 y = 20 After x--: Value of x = 9
Java Decrement Operator: Example 2
Output :
Before --x: Value of x = 10 y = 19 After --x: Value of x = 9