# Unary Operator in Java | Example Program

The operator that acts on a single operand is called unary operator in Java. A unary operator uses a single variable or literal.

For example, to assign two variables with different values containing positive or negative signs, the statements are:

```int x = -10; // Assign 'x' x negative 10.
int y = +20; // Assign 'y' y positive 20 (the plus is not required).
```

We can also do arithmetic operations on a single operand using unary operators. For example, the statement x = x + 1 has the same result as expression x++.

## Types of Unary Operators in Java

There are three types of unary operators in java. They are as follows:

1. Unary minus operator ( – )
2. Increment operator ( ++ )
3. Decrement operator ( – – )

## Unary minus operator in Java

The operator which is used to negate a value is called unary minus operator in java. When we use unary minus operator before a single operand, it performs unary negation.

In other words, it converts a positive value to an equivalent negative value and vice versa. Let’s understand it with an example program.

Program code 1:

```package unaryOperatorPrograms;
public class MinusExample
{
public static void main(String [] args)
{
int x = 5;
x = -5;
System.out.println(x);
}
}```
```Output:
-5```

In this code, the value of variable x is 5 at the beginning. When we have applied the unary minus ( – ) operator on it, then it became -5 because the unary minus operator has negated its value.

Negation means the conversion of negative value into positive value and vice versa.

## Increment Operator (++) in Java

Increment operator in Java is a unary operator that increases the value of a variable (operand) by one. In simple words, it increments a number by 1. It is represented by ( ++ ).

The operator ++ adds 1 to the operand. It can have the following forms:

```++x; or x++;
++x is equivalent to x = x + 1; (or x += 1; ).
```

Consider the following examples:

```1. int a = 5;
++a;
2. int a = 5;
a++
```

Here, the value of variable a is incremented by 1 when ++ operator is used before or after x. In Java, both are valid expressions.

Let’s take one more example to understand it better.

```a = a + 1;
```

In this expression, if a is equal to 5, then a + 1 value will be 6. This value will be stored again left-hand side of variable x. Thus, the value of x now becomes 6. We can perform the same thing by using ++ operator as well.

In Java, we can use the increment (++) operator in two forms:

• Pre-increment unary operator (Prefix)
• Post-increment unary operator (Postfix)

Let’s understand both forms one by one with the help of examples.

## Pre-increment Operator (Prefix) in Java

When we write ++ operator before a variable, it is called pre incrementation or prefix. In pre-increment, the increment is done first and then returns the new incremented value.

In other words, the prefix unary operator first adds 1 to the value of an operand, and then the result is stored in the left-hand side variable.

The general syntax of prefix or pre increment unary operator is as:

`++x;`

Let’s take a simple example program based on Java pre increment operator (++).

Program code 2:

```public class PreInr {
public static void main(String[] args)
{
int x = 2;
int y = ++x;
System.out.println("x = " +x+ ", " +"y = " +y);
}
}
```
```Output:
x = 3, y = 3
```

The first statement in the code assigns a value 2 to x. The second statement in the code performs two different actions:

• Increments x to 3.
• Assigns the new incremented value 3 to y.

## Post increment Operator (Postfix) in Java

When we write ++ operator after a variable, it is called post increment or postfix. In post-increment, the operator first returns the value of operand and then, at the end, an increment is done.

In other words, the postfix unary operator first stores the value in the left-side variable and then the operator adds 1 to an operand. The general syntax of post incrementing operator is as:

`x++;`

Let’s take an example program based on Java post increment operator.

Program code 3:

```public class PostInr {
public static void main(String[] args)
{
int x = 2;
int y = x++;
System.out.println("x = " +x+ ", " +"y = " +y);
}
}
```
```Output:
x = 3, y = 2
```

As you can observe in this program, the post increment operator first stores the value in the left-side variable y and then the operator increments the value of x by 1.

Let’s take another example based on both prefix and postfix increment operators.

Program code 4:

```package operatorPrograms;
public class IncrOperatorTest1
{
public static void main(String[] args)
{
int i = 0;
System.out.println(i); // Output 0
System.out.println(++i); // Output 1, first increment the value of i and then displays it as 1.

System.out.println(i); // Output 1, displays the value of i because i is already incremented, i.e. 1 at right-hand side.
System.out.println(i++); // Output 1, first displays the value of i as 1 and then increment it.
System.out.println(i); // Output 2, displays the incremented value of i, i.e. 2.
}
}```
```Output:
0
1
1
1
2```

Explanation:

1. The first pre-increment operator increases the value of i by 1 and outputs the new value of 1.

2. The next post-increment operator also increases the value of i by 1 but outputs the value of i as 1 before increment occurs.

3. After the post-increment, the output is displayed as 2.

Program code 5:

```package operatorPrograms;
public class IncrOperatorTest2
{
public static void main(String[] args)
{
int x = 50;
int y = 100;
int z = 200;

int a, b, c;
a = ++x;
b = y++;
c = x + y++ + ++z;

System.out.println("x = " +x);
System.out.println("y = " +y);
System.out.println("z = " +z);

System.out.println("a = " +a);
System.out.println("b = " +b);
System.out.println("c = " +c);
}
}```
```Output:
x = 51
y = 102
z = 201
a = 51
b = 100
c = 353```

Explanation:

In the above example program, the initial values of x, y, z are 50, 100, and 200 respectively.

1. When the statement a = ++x is executed, the value of x is incremented first by 1 and then the value of x is displayed as 51.

After incrementing, the value of x is stored into the variable a. Therefore, the value of a is also displayed as 51 (same as that value of x). Thus, this operation happened in the below form:

```x = x + 1;
a = x;
```

2. When the statement b = y++ is executed, the current value of y is stored into the variable b because the increment operator is in postfix form. Therefore, the value of b is displayed as 100.

After storing the current value of y into b, y’s value is incremented by 2 because y is modified two times in the program and then it is assigned to y. Therefore, the value of y is displayed as 102.

Thus, this operation occurs as follows:

```b = y;
y = ( y + 1) + 1;
```

3. When the statement c = x + y++ + ++z is executed, the value of x is assigned as 51 because the value of x is now 51 after increment. The value of y is incremented by 1 and then it is assigned as 101 into y because y++ is in postfix form.

Similarly, the value of z is also incremented by 1 and then it is assigned 201 into z. The sum of three values 51 + 101 + 201 will be stored into the variable c. Thus, the output is 353.

Let’s find the value of the following expression ++x*x++, given that the value of x is 10.

Program code 6:

```package operatorPrograms;
public class IncrOperatorTest3 {
public static void main(String[] args)
{
int x = 10;
int m = ++x * x++;
System.out.println("m = " +m);
}
}```
```Output:
m = 121```

Explanation:

1. In the above program, the value of variable x is 10. When ++x is executed, the operator will increment the value of x by 1, and then it will assign the value of x as 11.

2. When x++ is executed, the value of x will not be incremented in the statement because this is post incrementation. Hence, the value of x will stay the same, i.e. 11. Thus, the output will be 11 * 11 = 121.

Find the value of x and y in the following expression, given that the value of x is equal to 20.

Program code 7:

```package operatorPrograms;
public class IncrOpertaorTest4
{
public static void main(String[] arg)
{
int x = 20;
int y = ++x * 10 / x++ + ++x;
System.out.println("x = " +x);
System.out.println("y = " +y);
}
}```
```Output:
x = 23
y = 33```

Explanation:

This question is more complicated than the previous example. So, let’s understand the explanation of the above program.

1. When ++x will be executed in the numerator of expression, first, value’s x is incremented by 1 and then returned to the expression as 21, which is multiplied by 10.

So, the operation will occur like this: int y = 21 * 10 / x++ + ++x; // x assigned value of 21.

2. Next, when x++ in the denominator of expression will be executed, the value of x is again incremented by 1 but the original value 21 of x will use in the expression because it is post-increment.

So, the next operation will be like this: int y = 21 * 10 / 21 + ++x; // x assigned value of 21.

3. The final assignment of x increments the value of x by 1 because it is pre-increment. So, the value of x is now 23 because, after post-increment, the value of x returned as 22 to the ++x.

We can simplify this: int y = 21 * 10 / 21 + 23; // x assigned value of 23.

4. Finally, we can easily evaluate multiply and division from left to right and perform simple addition. Thus, the final value of x will be printed as 23, and the value of y is 33.

## Decrement Operator (- -) in Java

Decrement operator (- -) in Java is a unary operator that decrements the value of a variable (operand) by one. It is represented by ( – – ).

The operator – – subtracts 1 to the operand. It can have the following forms:

```--x; or x--;
--x is equivalent to x=x - 1; (or x -=1; ).
```

Like ++ operator, we can also use – – operator in two forms:

• Pre-decrement unary operator (Prefix)
• Post-decrement unary operator (Postfix)

Let’s understand both forms with example programs.

## Pre-decrement Operator in Java

When we write – – operator before a variable, it is called pre decrement or prefix. In pre-decrement, the decrement is done first and then returns a new decremented value.

In other words, the prefix operator first subtracts 1 to the value of a variable, and then the result is stored in the left-hand side variable. The general syntax of using pre decrement operator is as:

`--x;`

Let’s take a simple example program based on pre-decrement operator in Java.

Program code 8:

```package operatorPrograms;
public class PredecrementEx {
public static void main(String[] args)
{
int x = 10;
int y = --x;
System.out.println("x is " +x+ ", y is "+y);
}
}
```
```Output:
x is 9, y is 9
```

Inside the program code, the first statement assigns the value 10 to x. The second statement in the program performs two different actions:

• Decrements x to 9.
• Assigns a new decremented value to y.

## Post-decrement Operator in Java

When we write – – operator after a variable, it is called post decrement or postfix. In post-decrement, the operator first returns the value of operand and then at the end, decrement is done.

In other words, the postfix operator first stores the value in the left-side variable and then the operator subtracts 1 to the value of an operand. Post decrement is performed after all other operations are carried out.

Consider the following examples:

```1. int a = 5;
int p = --a; // pre-decrement.```

Here, the value of variable a is decremented by 1 and then assigned to a. In both cases, the value of a and p would be 4. The operation will occur like this;

```a = a - 1;
a = 5 -1;
a = 4;
p = 4;```
```2. int a = 5;
int q = a--; // post-decrement.```

Here, first, the original value of a will be assigned to variable q and then it is decremented by 1. In this case, the value of a is 4, and q is 5. The operation will be like this:

```q = 5;
a = a - 1;
a = 5 -1;
a = 4;```

Let’s take a simple example program based on the pre and post decrement operator.

Program code 9:

```package operatorPrograms;
public class DecrOperatorTest1
{
public static void main(String[] args)
{
int a = 1;
System.out.println(a); // Output 1
System.out.println(--a); // Output 0

System.out.println(a); // Output 0
System.out.println(a--); // Output 0
System.out.println(a); // Output -1
}
}```
```Output:
1
0
0
0
-1```

Try It Yourself

Program code 10:

```package operatorPrograms;
public class DecrOperatorTest2 {
public static void main(String[] args)
{
int a = 1;
++a;

int b = a++;
a--;

int c= --a;
int x = a * 10 / (b - c);

System.out.println("a = " +a);
System.out.println("x = " +x);
}
}```
```Output:
a = 1
x = 10```

Hope that this tutorial has covered all the important points related to the unary operator in Java. I hope you will have understood unary minus operator, pre-increment, and post-increment in Java nicely and enjoyed programming. In the next, we will discuss different types of conditional operator in Java.