Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculation and functions be it logical, arithmetic, relational etc. They are classified based on the functionality they provide. Here are a few types:
- Arithmetic Operators
- Unary Operators
- Assignment Operator
- Relational Operators
- Logical Operators
- Ternary Operator
- Bitwise Operators
- Shift Operators
This article explains all that one needs to know regarding the Unary Operators.
Unary Operators in Java
Java unary operators are the types that need only one operand to perform any operation like increment, decrement, negation etc. It consists of various arithmetic, logical and other operators that operate on a single operand. Let’s look at the various unary operators in detail and see how they operate.
- Unary minus(-): This operator can be used to convert a negative value to a positive one.
Syntax:
-(operand)
Example:
a = -10
Below is the program to illustrate Java unary – operator.
// Java code to illustrate unary -importjava.io.*;classUnary {publicstaticvoidmain(String[] args){// variable declarationintn1 =20;System.out.println("Number = "+ n1);// Performing unary operationn1 = -n1;// Print the result numberSystem.out.println("Result = "+ n1);}}chevron_rightfilter_noneOutput:Number = 20 Result = -20
- ‘NOT’ Operator(!): This is used to convert true to false or vice versa. Basically it reverses the logical state of an operand.
Syntax:
!(operand)
Example:
cond = !true; // cond < false
Below is the program to illustrate Java unary ! operator.
// Java code to illustrate// unary NOT operatorimportjava.io.*;classUnary {publicstaticvoidmain(String[] args){// initializing variablesbooleancond =true;inta =10, b =1;// Displaying cond, a, bSystem.out.println("Cond is: "+ cond);System.out.println("Var1 = "+ a);System.out.println("Var2 = "+ b);// Using unary NOT operatorSystem.out.println("Now cond is: "+ !cond);System.out.println("!(a < b) = "+ !(a < b));System.out.println("!(a > b) = "+ !(a > b));}}chevron_rightfilter_noneOutput:Cond is: true Var1 = 10 Var2 = 1 Now cond is: false !(a < b) = true !(a > b) = false
- Increment(++): It is used to increment the value of an integer. It can be used in two separate ways:
- Post-increment operator: When placed after the variable name, the value of the operand is incremented but the previous value is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement.
Syntax:
num++
Example:
num = 5 num++ = 6
- Pre-increment operator: When placed before the variable name, the operand’s value is incremented instantly.
Syntax:++num
Example:
num = 5 ++num = 6
Below is the program to illustrate Java unary Increment(++) operator.
// Java code to illustrate increment operatorimportjava.io.*;classUnary {publicstaticvoidmain(String[] args){// initializing variablesintnum =5;// first 5 gets printed and then// increment to 6System.out.println("Post "+"increment = "+ num++);// num was 6, incremented to 7// then printedSystem.out.println("Pre "+"increment = "+ ++num);}}chevron_rightfilter_noneOutput:Post increment = 5 Pre increment = 7
- Post-increment operator: When placed after the variable name, the value of the operand is incremented but the previous value is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement.
- Decrement(
--): It is used to decrement the value of an integer. It can be used in two separate ways:- Post-decrement operator: When placed after the variable name, the value of the operand is decremented but the previous values is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement.
Syntax:num--
Example:
num = 5 num-- = 4
- Pre-decrement operator: When placed before the variable name, the operand’s value is decremented instantly.
Syntax:--num
Example:
num = 5 --num = 4
Below is the program to illustrate Java unary Decrement(
--) operator.// Java code to illustrate decrement operatorimportjava.io.*;classUnary {publicstaticvoidmain(String[] args){// initializing variablesintnum =5;// first 5 gets printed and then// decremented to 4System.out.println("Post "+"decrement = "+ num--);System.out.println("num = "+ num);// num was 4, decremented to 3// then printedSystem.out.println("Pre "+"decrement = "+ --num);}}chevron_rightfilter_noneOutput:Post decrement = 5 num = 4 Pre decrement = 3
- Post-decrement operator: When placed after the variable name, the value of the operand is decremented but the previous values is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement.

