Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Unary Plus (+)
3.
Unary Minus (-)
4.
Increment Operator(++)
4.1.
Pre-Increment Operator
4.2.
Post-Increment Operator
5.
Decrement Operator(--)
5.1.
Pre-Decrement Operator
5.2.
Post-Decrement Operator
6.
Logical Complement Operator
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

Unary Operator

Author ANKIT KUMAR
1 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

The unary operator in Java is an operator that is used with only one operand. It is used to represent a positive or negative value, to increase or decrease the value by one, and to complement a Boolean value, i.e., change true to false and false to true.

The five unary operators in Java are:

  1. Unary Plus.
  2. Unary Minus.
  3. Increment operator
  4. Decrement Operator.
  5. Logical Complement Operator.

In this article, we shall discuss all of the above unary operators extensively with examples of each of them.

Recommended Topic-  Iteration Statements in Java, and Duck Number in Java.

Unary Plus (+)

The unary plus simply represents positive values. For example, 5 can be represented as +5. However, we generally do not use the plus sign before a positive value.

Syntax:

+(operand)

Example:

public class Main{
    public static void main(String args[]){
        
        int firstVariable= 5;
        
        // the second variable is assigned the value using unary plus
        int secondVariable=+5;
        
        System.out.println(firstVariable);
        System.out.println(secondVariable);
        
        // The result is 5 in both the case.
    }
}

Output: 

5
5

Also see, Swap Function in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Unary Minus (-)

This operator converts a positive number into a negative number. It is equivalent to multiplying with -1. For example -5, -x.

Syntax:

-(operand)

Example:

public class Main{
    public static void main(String args[]){
        
        int firstVariable= 5;
        
        // the second variable is assigned the value using unary minus
        int secondVariable= -5;
        int thirdVariable= - firstVariable;
        
        System.out.println(firstVariable);
        System.out.println(secondVariable);
        System.out.println(thirdVariable);
        
       //the value of the second variable and third variable is -5
    }
}

Output: 

5
-5
-5

Increment Operator(++)

The increment operator is used to increase the value of an operand by one. It is represented by ++. Based on the way the value is increased, the increment operator is of two types.

Pre-Increment Operator

Whenever ++ is written before a variable or an operand, it is known as a pre-increment operation. In the pre-increment operation, first, the value of the operand is increased by one, and then it is taken for any computation or arithmetical operation.

Syntax:

++(operand)

Examples: ++a, ++5.

Example:

class CodingNinjas{
    public static void main(String args[]){
        int val= 20;
        
        //pre-increment, equivalent to val=val+1
        ++val;
        System.out.println("After pre-increment the value is: "+val);
    }
}

Output: 

After pre-increment the value is: 21

Explanation:

  • First, the value of the variable val is assigned as 20.
  • When we write ++val, it is equivalent to writing val= val+1.
  • The value of val increments by one and becomes 21.
  • Finally, the output is 21.

Post-Increment Operator

Whenever ++ is written after a variable or an operand, it is known as a post-increment operation. In the post-increment operation, the value of the operand is first used for computation or any other operation and then incremented by one.

Syntax:

(operand)++

Example: a++, 5++.

Example:

class CodingNinjas{
    public static void main(String args[]){
        int val= 20;
        
        //post-increment
        //first the value of val is used by temp
        //and then incremented by one
        int temp=val++;
        
        //the temp holds the value 20
        System.out.println("Value of temp: "+temp);
        //value of val is 21
        System.out.println("Value of val after post-increment:"+val);
    }
}

Output: 

Value of temp: 20
Value of val after post-increment:21

Explanation:

  • The variable val is assigned a value 20.
  • When we post-increment val at that moment, the value of val is 20 only. It is stored in temp.
  • After that val is incremented by one.

The pre-increment operator first increments the value of the operand and then uses the operand for computation, whereas the post-increment operator first uses the operand for computation and then increments its value by one.

You can also read about Java Destructor and Hashcode Method in Java.

Decrement Operator(--)

The decrement operator is used to decrease the value of an operand by one. It is represented by --. Based on the way the value is decreased, the increment operator is of two types.

Pre-Decrement Operator

Whenever -- is written before a variable or an operand, it is known as a pre-decrement operation. In the pre-decrement operation, first, the value of the operand is decreased by one, and then it is taken for any computation or arithmetical operation.

Syntax:

--(operand)

Example: --a, --6

Example:

class CodingNinjas{
    public static void main(String args[]){
        int val= 20;
        
        //pre-decrement val
        //equivalent to val= val-1
        --val;
        
        System.out.println("The value of val after pre-decrement is: "+val);
        
    }
}

Output: 

The value of val after pre-decrement is: 19

Explanation:

  • The variable val is assigned a value 20.
  • The pre-decrement operation is performed on val by using --val.
  • The value of val decrements by 1. Now the value of val is 19.
  • Finally, 19 is printed.

Post-Decrement Operator

Whenever -- is written after a variable or an operand, it is known as a post-decrement operation. In the post-decrement operation, the value of the operand is first used for computation or any other operation and then decremented by one.

Syntax:

(operand)--

Example: a--., 5--

Example:

class CodingNinjas{
    public static void main(String args[]){
        int val= 20;
        
        //prints 20
        System.out.println(val--);
        
        //prints 19
        System.out.println(val);
        
    }
}

Output: 

20
19

Explanation:

  • The variable val is assigned a value equal to 20.
  • For the first print statement, the value of val remains unchanged since it is a post-decrement operation.
  • For the second print statement, the value of val decrements by one, and the new value of val is 19.

Logical Complement Operator

The logical complement operator reverses the value of an operand. If the value of the operand is true, then by using the logical complement operator with the operand, we change its value to false and vice-versa.

Syntax:

!(operand)

Example: !true, !false

Example: 

class CodingNinjas{
    public static void main(String args[]){
        
        boolean val1= true;
        boolean val2= false;
        
        //val1 is true, therefore !val1= false
        //prints false
        System.out.println(!val1);
        
        //val2 is false, therefore !val2= true
        //prints true
        System.out.println(!val2);
    }
}

Output: 

false
true

 

Try it on online java compiler.

Explanation:

  • The value of val1 is true. Therefore the complement of val1 will be false.
  • The value of val2 is false. Therefore the complement of val2 will be true.

 

Must Read Conditional Statements in Java

FAQs

1.What are unary operators in Java?

The unary operator in Java is an operator that is used with only one operand.

2. How many types of unary operators are there?

There are five types of unary operators. They are plus operator, minus operator, increment operator, decrement operator, and logical complement operator.

3. What is the difference between pre-increment and post-increment?

In pre-increment operation, the value of the operand is first incremented by one and then used for any other purpose, but in post-increment, first the value of the operand is used and then incremented by one.

4. What is the use of a logical complement operator?

It is used to reverse the value of the operand. It changes the true value to false and vice-versa.

Key Takeaways

In this article, we have extensively discussed unary operators in Java and its various code implementations.

  • The unary operator in Java is an operator that is used with only one operand.
  • There are five types of unary operators. They are plus operator, minus operator, increment operator, decrement operator, and logical complement operator.


Check out this problem - First Missing Positive 

We hope that this blog has helped you enhance your knowledge regarding unary operators and if you would like to learn more, check out our articles here. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Arithmetic Operator
Next article
Logical AND operator &&
Live masterclass