Unary Operators in Java with Example | Types of Java Unary Operators with Syntax & Examples

Java is a vast programming language that includes various topics like variables, data types, operator precedence, and arithmetic operators in java. We have given all these tutorials on our site previously. Now, in this tutorial, we will be going to learn about Unary Operators in Java. Full-fledge details can be discussed through this tutorial like Types of Java Unary Operators and their declarations with examples, etc. Make use of the below links & gain efficient knowledge on Unary Operators of Java.

This Java Unary Operators Tutorial Contains:

Unary Operators in Java

In Java, a Unary Operator is defined as an operator or a method that uses only one operand and simply performs different operations like positive value, negative value, incrementing/decrementing a value by one, negating an expression, or changing the value of a boolean.

In this tutorial, we will explain all types of unary operators in Java with examples and also help you to grasp the differences between i++ and i+=1 thoroughly.

Java Unary Operators Example:

class UnaryDemo {
public static void main(String[] args) {

int result = +1; // unary plus operator
// result is now 1
System.out.println("Result is now: " +result);

result--; // decrement operator
// result is now 0
System.out.println("Result is now: " +result);

result++; // increment operator
// result is now 1
System.out.println("Result is now: " +result);

result = -result; // unary minus operator
// result is now -1
System.out.println("Result is now: " +result);

boolean success = false;
// false
System.out.println("The result is: " +success);
// true
System.out.println("The result is: " +!success); //Logical complement operator.
}
}

Output:

Unary Operators in Java with Example 2

Also Check: 

Types of Unary Operators in Java

Unary Operators are of five types where they execute different operations which are discussed in a detailed way below. Here, we have given an image of Java Unary Operator Types along with the table.

types of unary operator in java

Given below is the list of five Unary Operators that provide the short description along with examples in a tabular form:

Operator Name Symbol Description Example Equivalent Expression
Unary Plus + It represents the positive value. +a a
Unary Minus It represents the negative value. -a
Increment Operator ++ Increments the value of a variable by 1. ++a or a++ a=a+1
Decrement Operator Decrements the value of a variable by 1. –a or a– a=a-1
Logical Complement Operator ! Inverts the value of a boolean variable. !true

Unary Plus(+) Operator

By using this unary plus operator, it gives Positive values.

Syntax:

+(operand)

Example:

x=+99 or x=99;

Unary Minus(-) Operator

By using this unary minus operator, the value will change into a Negative Value. If the given value is negative then it changes to a Positive Value.

Syntax:

-(operand)

Example:

a = -10

Increment and Decrement Operators in Java | Java Unary Operators (post and pre)

In Java Programming Language, there are various operators utilized respectively for implementing the logic of the given requirement. Java Unary Operators plays a key role in programming. In Unary Operators, there are various types and various forms like PostFix and PreFix (++ and –). Also, these are known as increment and decrement operators respectively.

It works on a single operand. Increment Operator (++) adds 1 to operand and Decrement Operator (–) subtracts 1 to the operand. If we use ++ as prefix (like: ++i), ++i will increment the value of i and then return it but, if ++ is used as postfix (like: i++), it will return the value of operand first and then only increment it.

OPERATOR EXAMPLE DESCRIPTION
++ [prefix] ++a The value of ‘a’ after the increment
++ [postfix] a++ The value of ‘a’ before an increment
— [prefix] –a The value of ‘a’ after decrement
— [postfix] a– The value of ‘a’ before the decrement

Difference Between i++ and i+=1 Unary Operators

Both i++ and i+=1 expression give the same result but the main difference between them is i++ uses the increment operator (unary operator) and i+=1 uses the assignment operator.

One more difference is i++ expression right away increments the value of ‘i’ whereas the i+=1 expression first converts into i=i+1 by the JVM and then evaluates the result.

Example on Increment and Decrement Operators in Java

class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++; //post increment
// prints 4
System.out.println("The value of post increment is: " +i);
++i; // pre increment
// prints 5
System.out.println("The value of pre increment is: " +i);
// post decrement, prints 5
System.out.println("The value of post decrement is: " +i--);
// pre decrement, prints 3
System.out.println("The value of pre decrement is: " +--i);

}
}

Output:

Unary Operators in Java with Example 3

Memorize 

  • In Java, the Nesting of increment or decrement operator is not supported. For instance:
int a=++(++b);  //not allowed
  • Never use unary operators with the final
  • The expressions ++x and x++ is the same as x=x+1 and the expressions – -x and x- – is the same as x=x-1

Logical Complement Operator

Java Unary Logical Complement Operator is performed to reverse the value of an operand. Thus, in case an operand has the value true, the complement operator will return the value as false and vice-versa. The representation of the Logical Complement Operator is by the exclamatory symbol (!).

Syntax:

!(operand)

Example:

flag=!true;