Java

Unary Operators in Java | Explained

Java supports various categories of operators like arithmetic, relational, unary, etc. The unary operators act differently from other operators as they require only one operand to perform a specific operation. The unary operators are used to change the sign of the operand, increment/decrement an operand or reverse a Boolean value. This guide of unary operators would serve the following learning outcomes:

  • getting aware of the working of unary operators
  • using several unary operators in Java

How unary operators work in Java

The syntax of each unary operator supported by Java is defined here that would assist you in getting the basic working flow of unary operators.

Plus(+): This operator is practiced to represent positive operand. The following syntax is followed to use this:

+operand;

Minus(-): This operator changes the sign of an operand and is used by following the syntax provided below:

-operand;

Increment(++): This unary operator adds 1 to the operand by prefixing or post fixing the “++” to the operand. The following syntax refers to postfix and prefix increment:

++operand;    //prefix increment
operand++;    //postfix increment

Decrement(–): The unary decrement subtracts 1 from the current value of the variable/value. It can be used by prefixing or post fixing the “–” to an operand. The syntax provided below is used for unary decrement:

--operand;    //prefix decrement
operand--;    //postfix decrement

Note: If a variable is associated with postfix that means the variable will first be used for computing and then its value will be incremented/decremented. Whereas the prefix increments/decrements the value before computing.

Logic complement(!): This operator is applicable on Boolean values and alters them from true to false and vice versa. The syntax provided below is followed for this:

!operand;

How to use the unary operators in Java

This section provides several Java examples that demonstrate the usage of unary operators in Java.

Using the unary plus: The unary plus does not make a change to the value as the positive sign is optional and thus it is not displayed anywhere after assignment. The following Java code declares a variable a with +5, but when it is used after that the positive sign is exempted.

package newpack;

public class UnOp {

public static void main(String[]args) {
       
    int a=+5;
   
    System.out.println(a);
    }
}

The output shows that the positive sign is exempted when the a is printed.

Using unary minus: The Java code provided below practices the unary minus on both positive and negative values.

package newpack;

public class UnOp {

public static void main(String[]args) {
       
    int a=5, b=-6;
   
    //using unary minus on a
    a=-(a);
    System.out.println(a);
   
    //using unary minus on b
    b=-(b);
    System.out.println(b);
    }
}

The code is described below:

  • initializes a as positive and b as a negative value
  • applied unary minus on a and update a
  • prints the new value of a
  • applied unary minus on b and update the value of b
  • prints the new value of b

The output of the code is provided below:

Using prefix and postfix increment: The prefix and postfix increment operators increment the value by 1. But the prefix and postfix are used as per their requirement, as the prefix increments before the execution and postfix increments the value after execution of the variable.

To show the usage of postfix and prefix increment, the following lines of Java code are executed:

package newpack;

public class UnOp {

public static void main(String[]args) {
       
    int x=99, y=9;
   
    //using unary prefix increment on x
    ++x;
    System.out.println(x);
   
    //using unary postfix increment on y
    y++;
    System.out.println(y);
    }
}

The code is described as:

  • initializes two variables x and y
  • applied prefix increment on x
  • prints the value after the increment
  • using postfix increment on y
  • prints the incremented value of y

The output of the code is provided below:

Using prefix and postfix decrement: The concept of decrement is the same as increment, however, it decrements the value by 1. The code provided below decrements the value of variables by prefixing/postfixing –:

package newpack;

public class UnOp {

public static void main(String[]args) {
       
    int a=20, b=10;
   
    //using unary prefix decrement on a
    --a;
    System.out.println(a);
   
    //using unary postfix decrement on b
    b--;
    System.out.println(b);
    }
}

The above code,

  • firstly declare two variables a and b
  • using prefix decrement and print new value of a
  • Using postfix decrement and shows the updated value of b

The output of the code is provided below:

Using logic complement: The logic complement operator reverses the order of a Boolean value. The following Java code exercises the Boolean false to true and vice versa:

package newpack;

public class UnOp {

public static void main(String[]args) {
       

    //declaring a boolean variable
    boolean bool1=true, bool2=false;
   
    //using logic complement operator on bool1
    bool1=!bool1;
   
    System.out.println(bool1);
   
    //using logic complement operator on bool2
    bool2=!bool2;
   
    System.out.println(bool2);
    }
}

The code is described below:

  • declares two Boolean variables bool1 and bool2
  • applies “!” operator on bool1 and updates value of bool1
  • applies “!” operator on bool2 and updates value of bool2

The output of the code is displayed below:

Conclusion

The unary operators allow you to alter the sign, perform an increment/decrement, or change the Boolean value(true/false). This article provides the working as well as the usage of unary operators in Java. You would have learned the basic syntax of all the unary operators. For better understanding, the unary operators are exercised using examples of Java code.

About the author

Adnan Shabbir