Java : Unary Operators

The Unary Arithmetic Operators are used to increasing or decreasing the value of an operand by adding 1 to the variable, whereas the decrement operator decreases a value.

These two operators have two forms: Postfix and  Prefix to increment or decrement in appropriate variables. These two operators can be placed before or after variables. It’s called a prefix is placed before variable and called as postfix if placed after the variable.


val=a++; //Store the  value of "a" in val then increments.
val=a--; //Store the  value of "a" in val then decrements.

val=++a; //First increment in value of "a" then store in val.
val=--a; //First decrement in value of "a" then store in val.

See Also :

Unary Operator Example

public class UnaryOperatorTest {
public static void main(String[] args) {
int r = 8;
System.out.println("r=: " + r++);
System.out.println("r=: " + r);

int x = 7;
System.out.println("x=: " + x--);
System.out.println("x=: " + x);

int y = 9;
System.out.println("y=: " + ++y);

int p = 5;
System.out.println("p=: " + --p);


r=: 8
r=: 9