Java Unary Operators

Java Unary Operators

Unary Operators in Java are used to perform increment, and decrement operations, these operators require single operand only.

Operators in Programming:

Operators are used to perform operations on variables and values. These operations can be Mathematical, Assignment, Comparison, Logical etc…

Operations on Variables and Values:

We can use Operators on Variables and Values to perform operations
Ex:
int a=10;
int b=20;

int x = a*b;
int y= 15*25;
System.out.println(x); //200
System.out.println(y); //375
System.out.println(25%4);//1

Java Tutorial
Java Step by Step Tutorial

Operators in Java:

We have several categories operators in Java, Arithmetic Operators, Unary Operators, Assignment Operators, Comparison Operators, and Logical Operators etc…

Some people call that Unary Operators are part of Arithmetic Operators, Yes It may be sub category of Arithmetic Operators.

Java Unary Operators:

The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

+ Unary plus operator; indicates positive value (numbers are positive without this, however)
– Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
— Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a boolean

Example:

class UnaryOperators {

public static void main(String[] args) {

int val = +1;
System.out.println(val); // val is now 1

val–; //val minus minus
System.out.println(val); // val is now 0

val++;
System.out.println(val);//val is now 1

val = -val;
System.out.println(val); //val is now -1

boolean result = false;
System.out.println(result); //false
// true
System.out.println(!result);//true

}
}

Note:

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand.
The code val++; and ++val; will both end in val being incremented by one.
The code val–; and –val; will both end in val being decremented by one.

If you are just performing a simple increment/decrement, it doesn’t really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.

Example:

pre-increment and post-increment, and pre -increment and post-decrement

int x=10;

System.out.println(x);//10

x++; //post-increment
System.out.println(x); //11

++x; //pre-increment
System.out.println(x);//12

x–;  //x minus minus
System.out.println(x);//11

–x; // minus minus x
System.out.println(x);//10

System.out.println(x++);//10

System.out.println(x);//11

System.out.println(++x);//12


Use of increment / decrement operator in loop blocks

//Print 1 to 10 numbers using for loop

for (int i=1; i<=10; i++) {
System.out.println(i);
}

//Print 10 to 1 numbers using for loop

for (int i=10; i>=1; i–) {
System.out.println(i);
}

Java Operators

Follow me on social media: