Skip to content
Related Articles

Related Articles

Improve Article
Java Unary Operator with Examples
  • Difficulty Level : Basic
  • Last Updated : 20 Jul, 2020

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:

  1. Arithmetic Operators
  2. Unary Operators
  3. Assignment Operator
  4. Relational Operators
  5. Logical Operators
  6. Ternary Operator
  7. Bitwise Operators
  8. 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.

  1. 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 -
      
    import java.io.*;
      
    class Unary {
        public static void main(String[] args)
        {
      
            // variable declaration
            int n1 = 20;
      
            System.out.println("Number = " + n1);
      
            // Performing unary operation
            n1 = -n1;
      
            // Print the result number
            System.out.println("Result = " + n1);
        }
    }
    Output:
    Number = 20
    Result = -20
    
  2. ‘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 operator
      
    import java.io.*;
      
    class Unary {
        public static void main(String[] args)
        {
            // initializing variables
            boolean cond = true;
            int a = 10, b = 1;
      
            // Displaying cond, a, b
            System.out.println("Cond is: " + cond);
            System.out.println("Var1 = " + a);
            System.out.println("Var2 = " + b);
      
            // Using unary NOT operator
            System.out.println("Now cond is: " + !cond);
            System.out.println("!(a < b) = " + !(a < b));
            System.out.println("!(a > b) = " + !(a > b));
        }
    }
    Output:
    Cond is: true
    Var1 = 10
    Var2 = 1
    Now cond is: false
    !(a < b) = true
    !(a > b) = false
    
  3. Increment(++): It is used to increment the value of an integer. It can be used in two separate ways:
    1. 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
    2. 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 operator
      
    import java.io.*;
      
    class Unary {
        public static void main(String[] args)
        {
            // initializing variables
            int num = 5;
      
            // first 5 gets printed and then
            // increment to 6
            System.out.println("Post "
                               + "increment = " + num++);
      
            // num was 6, incremented to 7
            // then printed
            System.out.println("Pre "
                               + "increment = " + ++num);
        }
    }
    Output:
    Post increment = 5
    Pre increment = 7
    
  4. Decrement(--): It is used to decrement the value of an integer. It can be used in two separate ways:
    1. 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
    2. 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 operator
      
    import java.io.*;
      
    class Unary {
        public static void main(String[] args)
        {
            // initializing variables
            int num = 5;
      
            // first 5 gets printed and then
            // decremented to 4
            System.out.println("Post "
                               + "decrement = " + num--);
      
            System.out.println("num = " + num);
      
            // num was 4, decremented to 3
            // then printed
            System.out.println("Pre "
                               + "decrement = " + --num);
        }
    }
    Output:
    Post decrement = 5
    num = 4
    Pre decrement = 3
    
  • Bitwise Complement(~): This unary operator returns the one’s complement representation of the input value or operand, i.e, with all bits inverted, means it makes every 0 to 1, and every 1 to 0.
    Syntax:
    ~(operand)

    Example:

    a = 5 [0101 in Binary]
    result = ~5
    
    This performs a bitwise complement of 5
    ~0101 = 1010 = 10 (in decimal)
    
    Then the compiler will give 2’s complement
    of that number.
    2’s complement of 10 will be -6.
    result = -6

    Below is the program to illustrate Java unary ~ operator.




    // Java code to illustrate unary ~
      
    import java.io.*;
      
    class Unary {
        public static void main(String[] args)
        {
      
            // variable declaration
            int n1 = 6, n2 = -2;
      
            // Displaying numbers
            System.out.println("First Number = " + n1);
            System.out.println("Second Number = " + n2);
      
            // Performing bitwise complement
            System.out.println(n1 + "'s bitwise complement = " + ~n1);
            System.out.println(n2 + "'s bitwise complement = " + ~n2);
        }
    }
    Output:
    First Number = 6
    Second Number = -2
    6's bitwise complement = -7
    -2's bitwise complement = 1
    

    Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




    My Personal Notes arrow_drop_up
  • Recommended Articles
    Page :