The “Operators” are an essential part of any programming language as they help the user to perform specific operations dynamically. With the help of operators, the user can apply specific operations over the elements after gathering information. For instance, display the “white” color logo if the mode is dark else display the “black” color logo. This conditional operation or toggling from dark to white is easily identified via the bitwise operator which is one of the “Unary” operators.

This guide explains the usage and working of unary operators in Java.

How to Use Unary Operators in Java?

The “Unary” operator as their name suggests is the one that applies or accepts only one operand like increment or decrement operators. There are five “Unary” operators which are described in the below guide:

Unary Operator 1: Plus Operator

The “plus” operator is a commonly used operator that represents that the operand is a positive value holder. However, it is not usually used because an operand with no symbol is considered positive by default. Yet, while creating finance or currency-related sites like Binance, this operator helps a lot.

Unary Operator 2: Minus Operator

The unary “minus” operator represents the operand value with a “negative(-)” sign. It can also be used in the stock market or tracking record-related websites to display that the stock or specific item is decreasing.

For instance, consider the below example:

public class StringContainsNumber {
public static void main(String[] args) {
  int Ethereum = 22000;
  System.out.println("Your Investment with Etherium Before = " + Ethereum);

  Ethereum = - Ethereum;
  System.out.println("Your Investment with Ethereum Now = " + (Ethereum));
}
}

In the above code snippet, the “minus” operator is attached to the targeted random variable “Ethereum”. For comparison, the value of the “Ethereum” variable is displayed before and after the attachment of a “” operator, as shown in below figure:

Unary Operator 3: NOT(!) Operator

The “NOT” operator performs the negation of boolean values(true or false) kept by the variables. It is usually used when the user wants to check something using a reverse or opposite approach. For instance, if the value of an input field is not empty then enable the “submit” button. This checking process is done by the utilization of the “NOT” operator, it has many more use cases like when the user wants to display the opposite result obtained by the function:

public class StringContainsNumber {
public static void main(String[] args) {
  boolean targetVal = true;
  String x = "Hello World";
  System.out.println("Original Value -> " + targetVal);
  System.out.println("Value After Using NOT Operator -> " + (!targetVal));
  System.out.println("\nOrignal Result Returned by Method-> " + x.isEmpty());
  System.out.println("Result Returned by Same Method With the Not Operator -> " + (!(x.isEmpty())));
}
}

In the above code snippet:

  • First, create two Strings having the data type of boolean and a String with random data. Then, display the boolean type variable on the console with and without the “NOT” operator. 
  • Next, apply the “isEmpty()” method over the String type variable to check whether it is empty or not. Lastly, to modify or negate the returned result apply the “NOT” operator.

Output

Unary Operator 4: Increment Operator

The “Increment” operator allows the user to increment the provided numeric value to a factor of 1. It is the enhanced or shorthand form of the regular arithmetic plus(+) operator as the plus operator can also add the value of a variable by a factor of 1. 

There are two approaches in which the increment operator can be performed namely “Pre Increment” and “Post Increment”. The “Pre Increment” first performs the addition part and then adds the retrieved value in a provided variable. As demonstrated below:

++Variable  =>  Variable = 1 + Variable

On the other hand, the “Post Increment” starts by first displaying or putting the variable and then incrementing it by a factor of 1. The visual representation and equivalent version using the plus operator are shown below:

Variable++  =>  Variable  = Variable + 1

Let’s implement both discussed incrementation approaches in Java:

public class StringContainsNumber {
public static void main(String[] args) {
  int targetVal = 20;
  System.out.println("Value of Targeted Variable -> " + targetVal);
  System.out.println("Pre Increment the Value of Targeted Variable -> " + (++targetVal));
  System.out.println("Post Increment the Value of Targeted Variable -> " + (targetVal++));
  System.out.println("Value of Targeted Variable After Post Increment -> " + (targetVal));
}
}

The above code works like this:

  • First, define and print the targeted value on the console.
  • Next, apply the “Pre Increment” operation over the targeted value using the “increment (++)” operator.
  • Then, perform the “Post Incrementation” over the same variable and print it as it first displays the variable and then performs increment. So, the incrementation is not displayed in the variable output.
  • Lastly, display the targeted variable and now the previously done incrementation gets displayed.

The output shows the working difference between Post and Pre-Incrementation:

Unary Operator 5: Decrement Operator

The “Decrement(–)” operator works similarly to the previously described “Increment(++)” operator but instead of adding, it subtracts the value by a factor of “1”. It is also applied using two approaches namely “Pre-decrement” and “Post-decrement”.  The visual representation of these approaches along with their equivalent arithmetic “Subtract (-)” operator is shown below:

Approach NameUnary OperatorArithmetic Representation
Pre Decrement–variablevariable = 1 – variable
Post Decrementvariable–variable = variable – 1

Let’s implement the same example described in the above code using Decrement Operator:

public class StringContainsNumber {
public static void main(String[] args) {
  int targetVal = 20;
  System.out.println("Value of Targeted Variable -> " + targetVal);

  System.out.println("Pre Decrement the Value of Targeted Variable -> " + (--targetVal));
  System.out.println("Post Decrement the Value of Targeted Variable -> " + (targetVal--));

  System.out.println("Value of Targeted Variable After Post Decrement -> " + (targetVal));
}
}

The above code is the same as the one explained in the above section. Only modify the “++” operator with the “” operator to perform the decrementation operation. The “Post-Decrement” operator also first simply prints the variable that does not reflect the decrementation. Then, the variable is displayed again to get a visible change of decrementation.

Output:

That’s all on the usage of Unary Operators in Java.

Conclusion

In Java, the Unary operators are the one that performs operations only on a single operand or variable. There are five types of Unary operators namely “Plus Unary”, “Minus Unary”, “NOT or Bitwise”, Increment Unary”, and “Decrement Unary”. The “Plus” and “Minus” unary operators represent the positive or negative values in code. The “Bitwise or NOT” operators perform the negation of Boolean values like it converts the returned value of “true” into “false” and vice versa. The “Increment” and “Decrement” unary operators add or subtract the provided operand or variable value by a factor of 1. This guide has demonstrated the working of all unary operators provided by Java.