Unary Operators In Java And Its Types

By | September 27, 2023

Unary Operators

Unary Operators are special symbols or keywords used to perform operations on different operands, such as variables, literals, or expressions. They are fundamental parts of the Java programming language, which is used to manipulate data and perform various calculations. There are various types of operators in Java, such as arithmetic operators, assignment operators, relational operators, logical operators, unary operators, and others.

In this article, we will learn about unary operators in detail. You need to stay till the end to get a better understanding of this important operator in Java.

What are Unary Operators?

In Java, unary operators play a preeminent role. For performing any calculations, unary operators require only one operand to perform any operations like increment, decrement, negation, etc. It consists of various operators that support working on a single operand. Let us look at the various types of Unary operators in detail in this article. 

Increment Operator (++)

Post Increment(a++)

This operator is used to increment the existing value of the operand. There are two major types of increment operators. It happens when we put the variable name before the ++ sign, and then it updates after the execution is complete. 

Increment Unary Operator
public class IncrementExample {

    public static void main(String[] args) {

        int count = 5;

        

        count++;

        System.out.println(“The updated value of ‘count’ is: ” + count);

    }

}

Here, due to the post-increment operator count++ result in 6.

Output
The updated value of count is 6

Recommended Course 

Pre-Increment Operator(++a)

In this type of increment operator, the operator is placed before the name of the variable or operand. Incrementation happens instantly. Let us take the same example given above. If the operator (++) was before the variable name, the result would have differed. 

Pre-increment Operator
count=5;

++count;

 

Output
The updated value of count is 6

Decrement Operator (–)

The decrement operator is used to decrement the value of any integer by one. Just like the increment operator, it also has two types Post decrement and pre-decrement operators. 

Post-Decrement Operator

When the decrement operator is kept after the variable, the value is not changed instantly; however, it is retained until the statement gets executed, then after it gets incremented.

Post-Decrement Unary Operator
public class DecrementExample {

    public static void main(String[] args) {

        int count = 5;

        

        count – -;

        System.out.println(“The updated value of ‘count’ is: ” + count);

    }

}

 

Output
The updated value of count is 4

Pre-decrement Operator

This operator decrements the value of the operand instantly and does not wait for the execution to be completed.

Output
count=5

– – count;

 

Output
The updated value of count is 4

Unary Minus (-)

The unary minus operator converts the positive value to a negative value. However, using it with an already negative value will convert it into a negative number. 

Unary Minus Operator
int x = 10;

int y = -x;

System.out.println (y); 

The unary minus operator can be used to negate the result of any expression. 

Output
-10

NOT Operator (!)

The NOT operator in Java is a logical operator which is used to negate any boolean value. It can convert a true value to a false value or vice versa.

It is represented by an exclamation mark (!). It is a unary operator as it needs only one operand to operate. 

NOT Operator
boolean isTrue = true;

boolean isFalse = !isTrue;

System.out.println(isFalse); 

 

Output
true

Bitwise Complement (~)

The unary complement operator returns the one’s complement of any input bit value or operand. It generally works with bits and gives its ones complement as an output. The ~ operator represents it. It can change any bit from 1 to 0 or vice versa. 

Bitwise Complement OperatorBitwise Complement Operator (~)
int x = 10;

int y = ~x;

System.out.println(y); 

The bitwise complement operator inverts all the bits in the operand. Here, in 10, the binary representation is 00001010 which is converted to -11 having the binary representation as 11110101. 

Output
-11

Uses of Bitwise Complement Operator 

  • It is used in calculating the negative of an integer.
  • It is also used in creating masks to perform bit-level operations.
  • It can create random numbers and implement cryptographic algorithms too.

Advantages of Using Unary Operators In Java 

Unary operators in Java are a very powerful tool that can be used to simplify and improve our code. Here are some of the advantages of using unary operators in Java.

  • Efficiency: They help to improve efficiency in your code. For example, the minus unary operator can negate a value easily in a single step. It eliminates the complex process of creating objects.
  • Conciseness: It can help in making our code more concise and easy to read. For example, the increment operator can increment a variable by one which simplifies the complex syntax and makes it comfortable to use and understand.
  • Flexibility: It can be used with various types of data such as integers, floats, and, boolean values. It makes them a versatile tool for Java programmers.
  • Modification: It can also be used to change or modify the primitive data type values such as int, long, float, double, etc.
Unary Operators in Java Example
// Increment operator

int count = 5;

count++; // increments count to 6

// Decrement operator

count–; // decrements count to 5

// Unary minus operator

int x = -10; // negates 10 to -10

// NOT operator

boolean isTrue = true;

boolean isFalse = !isTrue; // inverts true to false

// Bitwise complement operator

int y = ~5; // bitwise complement of 5 is 10

UNARY OPERATORS FREQUENTLY ASKED QUESTIONS (FAQs)

Q1. What is the unary operators in Java?

Ans: A unary operator can perform different operations with only one operand. It can be used for various functions, such as increment, decrement, negate, invert boolean, and create masks.

Q2. What are the different types of unary operators in Java?

Ans: There are five types of unary operators in Java:

  • Increment Operator (++)
  • Decrement Operator (–)
  • Unary Minus Operator (-)
  • NOT Operator (!)
  • Bitwise Complement Operator (~)

Q3: What are some of the common mistakes we must avoid when using unary operators in Java?

Ans: Here are some common mistakes to avoid when using unary operators in Java:

  • Incorrect use of unary operators: Unary operators can only be used with specific data types. For example, only numeric data types are compatible with the increment and decrement operators.
  • Using unary operators in the wrong context: Unary operators can only be used in certain contexts. For example, the NOT operator can only be used with boolean expressions.
  • Misunderstanding the order of operations: unary operators have a higher precedence than arithmetic operators. This means that unary operators will be evaluated first, followed by arithmetic operators.
Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe
Scroll to Top
close
counselling
Want to Enrol in PW Skills Courses
Connect with our experts to get a free counselling & get all your doubt cleared.