Unary Operator in Java: Types, Examples

An operator that performs an operation on only a single operand is called a unary operator in Java. The term “unary” literally means “one.” Therefore, a unary operator works with a single variable or value. In other words, unary operators require only one operand to perform an operation.

Unary operators are extremely useful when performing operations such as incrementing counters, reversing boolean values, or manipulating bits in a program.

Consider an example:

int number = 5;
number++;

In this example, the ++ is a unary increment operator, which increases the value of number by one. Instead of writing a longer statement like number = number + 1; the unary increment operator performs the same task with a much shorter expression. Thus, unary operators in Java reduce clutter in the code and improve readability.

Types of Unary Operators in Java


Java provides several types of unary operators to perform a specific operation on a single operand. They are:

  1. Unary Plus (+)
  2. Unary Minus (-)
  3. Increment Operator (++)
  4. Decrement Operator (–)
  5. Logical NOT Operator (!)
  6. Bitwise Complement Operator (~)

Let us understand each operator with examples.

Learn unary operator in Java and its various types with examples.

Unary Plus Operator (+) in Java


The unary plus operator (+) is one of the simplest unary operators available in Java that represents a positive value. It does not change the value but indicates that the number is positive.

Example 1:

public class UnaryPlusExample {
   public static void main(String[] args) {
     int a = 10;
     int result = +a;
     System.out.println("Value of a: " + a);
     System.out.println("Unary plus result: " + result);
   }
}

Output:

Value of a: 10
Unary plus result: 10

In this example, the unary plus operator simply returns the positive value of the operand without changing it.

Unary Minus Operator in Java


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

In simple words, it converts a positive value to an equivalent negative value and vice versa. Let’s understand it with an example program.
[blocksy-content-block id=”12371″]

Example 2: Changing the Sign of a Number

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

Output:

-5

In this example, the value of variable x is 5 at the beginning. When we have applied the unary minus ( – ) operator on it, 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. The unary minus operator in Java simply flips the sign of the variable. If the number is positive, it becomes negative. If it is already negative, applying unary minus again makes it positive.

Increment Operator (++) in Java


The increment operator (++) is one of the most commonly used unary operators in Java. This operator increases the value of a variable (operand) by one. In simple words, the increment operator increments a number by 1. It is represented by (++).

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

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

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

Pre-Increment Operator in Java


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

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

The general syntax of the prefix or pre increment unary operator in Java is:

++x;

Here, x is a variable and the ++ is increment operator.

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

Example 2:

public class PreInr {
  public static void main(String[] args) 
  {
    int x = 2;
    int y = ++x; // Increment the value by 1
    System.out.println("x = " + x + ", " +"y = " + y);
  }
}

Output:

x = 3, y = 3

In this example, we have defined a variable named x and assigned a value 2 to it. After that, we have incremented the value by 1 and assigned the new incremented value 3 to the variable y.

Post-Increment Operator in Java


When we write the ++ operator after a variable, it is called post-increment or postfix increment. In post-increment, the operator first returns the original value of operand and then the increment operation is done.
[blocksy-content-block id=”12121″]
In other words, the postfix unary operator first assigns the original value to the left-hand side variable and then the operator adds 1 to the operand. Post-increment is performed after the value is used in the expression. The general syntax of post increment operator is:

x++;

Let us take an example program based on Java post-increment operator.

Example 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 example program, the post-increment operator first stores the current value in the left-hand side variable y and then the operator increments the value of x by 1.


Let us take another example program in which we will use both prefix and postfix increment operators in Java.

Example 4: Behavior of Prefix and Postfix Increment Operators

public class IncrOperatorTest1 { 
   public static void main(String[] args) 
   { 
      int i = 0; 
      System.out.println(i); 
      System.out.println(++i); 
      System.out.println(i);  
      System.out.println(i++); 
      System.out.println(i);  
   } 
}

Output:

0 
1 
1 
1 
2

In this example:

  • We have declared a variable named i and assigned the value 0 to it.
  • The statement System.out.println(i); prints the current value of i. Therefore, the output is 0.
  • The System.out.println(++i); first increments the value of i by 1, then prints it. Therefore, the output is 1.
  • The statement System.out.println(i); prints the updated value of i. Therefore, the output is 1.
  • System.out.println(i++); first prints the current value of i (which is 1), and then increments it. After execution ++, the value of i becomes 2. However, the output will be 1.
  • The statement System.out.println(i); prints the final value of i after post-increment. Therefore, the output is 2.

Advanced Examples of Pre-Increment and Post-Increment in Java


Example 5:

public class IncrOperatorExample { 
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

In this example:

  • The initial values of variables x, y, and z are 50, 100, and 200, respectively.
  • When the statement a = ++x is executed, the value of x is first incremented by 1, and then the updated value is assigned to the variable a. Since the initial value of x is 50, the incremented value of x is 51. Therefore, both x and a will have the value 51.
  • 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 prints 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 prints 102. Thus, this operation occurs as follows:
b = y;
y = ( y + 1) + 1;
  • 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.

[blocksy-content-block id=”12153″]

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

Example 6:

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

Output:

m = 121

In this example:

  • The initial value of variable x is 10. When the expression ++x is executed, the operator will increment the value of x by 1, and then it will assign the updated value 11 to the variable x.
  • When the expression 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.

Example 7:

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

In this example:

  • When the expression ++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.
  • 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.
  • 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.
  • 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 decreases the value of a variable (operand) by one. It is represented by  -- . The decrement operator (–) subtracts 1 from the operand.

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

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

Let us understand both forms with the help of example programs.

Pre-Decrement Operator in Java


When we write the -- operator before a variable, it is called pre-decrement or prefix decrement in Java. In pre-decrement, the value of the variable is decreased first and then the new decremented value is returned..

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

--x; // Pre-decrement

Let us consider a simple example program based on pre-decrement operator in Java.

Example 8:

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

In this example:

  • We have defined a variable named x and assigned the value 10 to it.
  • The expression –x; first subtracts 1 from the value of the variable x and then stores the result in the variable named y.

Post-Decrement Operator in Java


When we write the -- operator after a variable, it is called post-decrement or postfix decrement in Java. In post-decrement, the operator first returns the original value of the operand and then the decrement operation is performed.

In other words, the postfix operator first assigns the original value in the left-hand side variable, and then the operator subtracts 1 from the operand. Post-decrement is performed after the value is used in the expression.

The general syntax of using post-decrement operator is:

x--; // Post-decrement

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

Example 9: Behavior of Pre-decrement and Post-decrement Operators

public class DecrOperatorTest1 { 
   public static void main(String[] args) 
   { 
      int a = 1; 
      System.out.println(a); 
      System.out.println(--a); 
      System.out.println(a); 
      System.out.println(a--); 
      System.out.println(a); 
   } 
}

Output:

1 
0 
0 
0 
-1

Example 10: Try It Yourself

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

Logical NOT Operator (!) in Java


The logical NOT operator (!) is a unary operator, which reverses the boolean value. In other words, it reverses the logical state of a boolean expression. If the condition is true, the logical NOT operator makes it false. If the condition is false, the operator converts it into true. We usually use the logical NOT operator in Java conditional statements and loops.

The general syntax of using the logical NOT operator in Java is:

!condition

Example 11:

public class LogicalNotExample {
   public static void main(String[] args) {
      boolean isJavaEasy = true;
      System.out.println(!isJavaEasy);
   }
}

Output:

false

Bitwise Complement Operator (~) in Java


The bitwise complement operator (~) reverses all bits of a number. To understand how it works, you need to think in terms of binary numbers. Java stores every primitive integer type (byte, short, int, long) internally as a sequence of bits (0s and 1s) using the two’s complement.

The bitwise complement operator flips each of these bits—turning every 0 into 1 and every 1 into 0. Let’s understand it with the help of an example.

Example 12:

public class BitwiseComplementExample {
   public static void main(String[] args) {
     int x = 5;
     int result = ~x;
     System.out.println("Result: " + result);
   }
}

Output:

Result: -6

Why does this happen? Let’s break it down. The number 5 in binary (using 32-bit representation) looks like this:

00000000 00000000 00000000 00000101

When we apply the bitwise complement operator (~), every bit is flipped into:

11111111 11111111 11111111 11111010

This new binary value represents -6 in decimal form using two’s complement representation. So essentially, the formula becomes:

~n = -(n + 1)

Summary Table of Unary Operators

OperatorNameDescription
+Unary PlusIndicates positive value
Unary MinusChanges sign of number
++IncrementIncreases value by 1
DecrementDecreases value by 1
!Logical NOTReverses boolean value
~Bitwise ComplementFlips all bits
DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.