Unary Operators in Java

unary operators in java

Unary operators in Java work on a single operand to perform quick actions like incrementing, decrementing, or changing a value’s sign or logic. They make code simpler and faster by allowing small yet powerful operations with just one variable.

    In real life, there are simple single-step actions. Similarly, in programming we often need to increase, decrease or change the sign of a number using just one variable. This is where unary operators come in. They work on a single operand to perform simple yet powerful operations in Java.

What are Unary Operators?

    A Unary Operator is an operator that acts on only one operand. For example, in the expression x++, the operator ++ works only on x. Unary operators are used to increase, decrease, negate a value or to invert a condition.

Purpose of Unary Operators

    The main purpose of unary operators is to perform quick and efficient increment, decrement, and logical operations without writing lengthy code. They make expressions shorter, easier to understand, and faster to execute. For example:

count++;

    is much simpler and cleaner than:

count = count + 1;

Why Unary Operators Matter in Java

    Unary operators matter because they:

  • Simplify arithmetic and logical expressions.
  • Save time and reduce the chances of errors.
  • Are used in loops, conditions, and calculations frequently.
  • Help in writing compact and efficient code.
  • From incrementing loop counters to toggling logical conditions, unary operators play a vital role in controlling the program’s flow.

Types of Unary Operators in Java

    Java supports several types of unary operators, mainly divided into:

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

Unary Operators in Detail

1. Unary Plus (+)

    The unary plus (+) operator is used to indicate a positive value. It doesn’t change the value of the operand; it simply returns the number as it is. 

Syntax:    +operand

Example:

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

2. Unary Minus (-)

    The unary minus (-) operator is used to negate (change the sign) of a numeric value. If the operand is positive, it becomes negative. If the operand is negative, it becomes positive.

Syntax:    -operand

Example:

public class UnaryMinusExample {
    public static void main(String[] args) {
        int a = 10;
        int b = -a;
        System.out.println("Value of b: " + b);
  }
}

3. Increment Operator (++)

    The increment operator (++) is a unary operator that increases the value of a variable by 1. It can be used in two forms:

  • Pre-increment (++x) → Increases the value first, then uses it.
  • Post-increment (x++) → Uses the value first, then increases it.
a. Pre-increment (++x)

    Increases the value first, then uses it. Example:

public class IncrementExample {
    public static void main(String[] args) {
        int a = 5;
       int b = ++a;                      // a is increased first, then assigned to b
       System.out.println("After pre-increment:");
       System.out.println("a = " + a);  // 6
       System.out.println("b = " + b);  // 6
  }
}
b. Post-increment (x++)

    Uses the value first, then increases it. Example:

public class IncrementExample {
    public static void main(String[] args) {
       int a = 5;
       int c = a++;                      // a is assigned first, then increased
       System.out.println("\nAfter post-increment:");
       System.out.println("a = " + a);  // 6
       System.out.println("c = " + c);  // 5
   }
}

4. Decrement Operator (–)

    The decrement operator (–) is a unary operator that decreases the value of a variable by 1. It can be used in two forms:

  • Pre-decrement (–x) → Decreases the value first, then uses it.
  • Post-decrement (x–) → Uses the value first, then decreases it.
a. Pre-decrement (–x)

    Decreases the value first, then uses it. Example:

public class DecrementExample {
    public static void main(String[] args) {
        int a = 5;
        int b = --a;                     // a is decreased first, then assigned to b
        System.out.println("After pre-decrement:");
        System.out.println("a = " + a);  // 4
        System.out.println("b = " + b);  // 4
  }
}
b. Post-decrement (x–)

    Uses the value first, then decreases it. Example:

public class DecrementExample {
    public static void main(String[] args) {
        int a = 5;
        int c = a--;                  // a is assigned first, then decreased
        System.out.println("\nAfter post-decrement:");
        System.out.println("a = " + a);  // 4
        System.out.println("c = " + c);  // 5
  }
}

5. Logical Complement Operator (!)

     The logical complement operator (!) is a unary operator that works only with Boolean values. It inverts the value: true → false,false → true. It is often used in conditions to reverse logic.

Syntax:    !boolean_expression

Example:

public class LogicalComplementExample {
    public static void main(String[] args) {
        boolean flag = true;
        System.out.println("After applying ! : " + !flag);
        int a = 10, b = 20;
        System.out.println(!(a < b));      // false (since a<b is true, ! makes it false)
        System.out.println(!(a > b));      // true  (since a>b is false, ! makes it true)
    }
}

6. Bitwise Complement Operator (~)

     The bitwise complement operator (~) is a unary operator that works on integer types. It inverts each bit of the number:0 → 1,1 → 0

Syntax:    ~operand

Example:

public class BitwiseComplementExample {
    public static void main(String[] args) {
        int a = 5;
        int b = ~a;
        System.out.println("Value of ~a: " + b);
    }
}

Precedence and Associativity of Unary Operators

    In Java, unary operators have high precedence, which means they are evaluated before most other operators like arithmetic or relational operators. They also have right-to-left associativity, meaning if multiple unary operators appear in a single expression, the rightmost one is applied first. For example:

int x = 5;
int y = ++x + --x;
System.out.println(y);

    Here, ++x is applied first (making x = 6), then –x (making x = 5 again), and finally y = 6 + 5 = 11.

Final Thought

    Unary operators may look small, but they are extremely powerful tools in Java programming. They make code cleaner, faster, and easier to write. Whether you’re increasing a value, flipping a Boolean, or working with loops , unary operators help you get the job done with minimal effort. In short, a single symbol can make a big difference in the logic and flow of your Java program.


Share the Post:

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Posts​

factorization class 8 rs aggarwal

Factorization

Step by Step solutions of Exercise- 14A of RS Aggarwal ICSE Class-8 Maths chapter 14-...
Read More

Join Our Newsletter

Name
Email
The form has been submitted successfully!
There has been some error while submitting the form. Please verify all form fields again.
Scroll to Top