The operators which acts upon one operand is called Unary Operator. There are three types of Unary operators in java
This operator is used to negate a given value. Negation means to convert negative value into positive and Positive value into negative.
for example
int a=8;
int b=-4;
System.out.println(-a); will display -8
System.out.println(-b); will display 4
Increment Operator(++)
This value increases the value of variable by 1.for example
int a=3;
++a will make it a=4
a++ will make it a=5
if we use increment operator in left side of variable it is known as pre increment operator.pre increment operator first increment operator first increments the value then displays it.
if we use increment operator in right side of variable it is called post increment operator. It first displays the value then increments it.
int a=2
System.out.println(a);
System.out.println(++a);
System.out.println(a);
Output:
2
3
3
int b=3
System.out.println(b);
System.out.println(b++);
System.out.println(b);
Output:
3
3
4
Decrement Operator(--)
This operator is used to decrement the value of variable by 1.for example
int s=2;
--s will make it s=1
s-- will make it s=0
if we use decrement operator in left side of variable it is known as pre decrement operator.pre decrement operator first decrements the value then displays it.
if we use decrement operator in right side of variable it is called post decrement operator. It first displays the value then decrements it.
int a=2
System.out.println(a);
System.out.println(--a);
System.out.println(a);
Output:
2
1
1
int b=3
System.out.println(b);
System.out.println(b--);
System.out.println(b);
Output:
3
3
2
- Unary minus operator(-)
- Increment Operator(++)
- Decrement Operator(--)
This operator is used to negate a given value. Negation means to convert negative value into positive and Positive value into negative.
for example
int a=8;
int b=-4;
System.out.println(-a); will display -8
System.out.println(-b); will display 4
Increment Operator(++)
This value increases the value of variable by 1.for example
int a=3;
++a will make it a=4
a++ will make it a=5
if we use increment operator in left side of variable it is known as pre increment operator.pre increment operator first increment operator first increments the value then displays it.
if we use increment operator in right side of variable it is called post increment operator. It first displays the value then increments it.
int a=2
System.out.println(a);
System.out.println(++a);
System.out.println(a);
Output:
2
3
3
int b=3
System.out.println(b);
System.out.println(b++);
System.out.println(b);
Output:
3
3
4
Decrement Operator(--)
This operator is used to decrement the value of variable by 1.for example
int s=2;
--s will make it s=1
s-- will make it s=0
if we use decrement operator in left side of variable it is known as pre decrement operator.pre decrement operator first decrements the value then displays it.
if we use decrement operator in right side of variable it is called post decrement operator. It first displays the value then decrements it.
int a=2
System.out.println(a);
System.out.println(--a);
System.out.println(a);
Output:
2
1
1
int b=3
System.out.println(b);
System.out.println(b--);
System.out.println(b);
Output:
3
3
2