What operator is a unary operator which means it works with only one operand?

Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types:

  1. Arithmetic Operators
  2. Unary Operators
  3. Assignment Operator
  4. Relational Operators
  5. Logical Operators
  6. Ternary Operator
  7. Bitwise Operators
  8. Shift Operators

Unary Operators in Java

Java unary operators are the types that need only one operand to perform any operation like increment, decrement, negation, etc. It consists of various arithmetic, logical and other operators that operate on a single operand. Let’s look at the various unary operators in detail and see how they operate. 

Operator 1: Unary minus(-)

This operator can be used to convert a positive value to a negative one. 

Syntax: 

-(operand)

Illustration: 

a = -10

Example:

Java




a = -10
7

 

a = -10
8

a = -10
9
Number = 20
Result = -20
0

 

Number = 20
Result = -20
1

Number = 20
Result = -20
2
Number = 20
Result = -20
3

 

Number = 20
Result = -20
4
Number = 20
Result = -20
5

Number = 20
Result = -20
4
Number = 20
Result = -20
7
Number = 20
Result = -20
8
Number = 20
Result = -20
9
!(operand)
0

Number = 20
Result = -20
4
!(operand)
2

!(operand)
3
!(operand)
4

!(operand)
3
!(operand)
6
!(operand)
7
!(operand)
8
!(operand)
9

 

!(operand)
3
cond = !true;
// cond < false
1

!(operand)
3
cond = !true;
// cond < false
3
cond = !true;
// cond < false
4
cond = !true;
// cond < false
5

 

!(operand)
3
cond = !true;
// cond < false
7

!(operand)
3
cond = !true;
// cond < false
9

 

!(operand)
3
Cond is: true
Var1 = 10
Var2 = 1
Now cond is: false
!(a < b) = true
!(a > b) = false
1

!(operand)
3
Cond is: true
Var1 = 10
Var2 = 1
Now cond is: false
!(a < b) = true
!(a > b) = false
3

!(operand)
3
cond = !true;
// cond < false
3
Cond is: true
Var1 = 10
Var2 = 1
Now cond is: false
!(a < b) = true
!(a > b) = false
6
cond = !true;
// cond < false
5

Number = 20
Result = -20
4
Cond is: true
Var1 = 10
Var2 = 1
Now cond is: false
!(a < b) = true
!(a > b) = false
9

Cond is: true
Var1 = 10
Var2 = 1
Now cond is: false
!(a < b) = true
!(a > b) = false
9

Output

Number = 20
Result = -20

Operator 2: ‘NOT’ Operator(!)

This is used to convert true to false or vice versa. Basically, it reverses the logical state of an operand.

Syntax: 

!(operand)

Illustration: 

cond = !true;
// cond < false

Example:

Java




num++
1

 

a = -10
8

a = -10
9
Number = 20
Result = -20
0

 

Number = 20
Result = -20
1

Number = 20
Result = -20
2
Number = 20
Result = -20
3

 

Number = 20
Result = -20
4
Number = 20
Result = -20
5

Number = 20
Result = -20
4
Number = 20
Result = -20
7
Number = 20
Result = -20
8
Number = 20
Result = -20
9
!(operand)
0

Number = 20
Result = -20
4
!(operand)
2

!(operand)
3
num = 5
num++ = 6
8

!(operand)
3
++num
0
++num
1
++num
2
!(operand)
9

!(operand)
3
!(operand)
6
++num
6
++num
7
++num
8
++num
9
!(operand)
9

 

!(operand)
3
num = 5
++num = 6
2

!(operand)
3
cond = !true;
// cond < false
3
num = 5
++num = 6
5
num = 5
++num = 6
6

!(operand)
3
cond = !true;
// cond < false
3
num = 5
++num = 6
9
a = -10
00

!(operand)
3
cond = !true;
// cond < false
3
a = -10
03
a = -10
04

 

!(operand)
3
num = 5
++num = 6
2

!(operand)
3
a = -10
08

!(operand)
3
cond = !true;
// cond < false
3
a = -10
11
a = -10
12

!(operand)
3
cond = !true;
// cond < false
3
a = -10
15
a = -10
16

!(operand)
3
cond = !true;
// cond < false
3
a = -10
19
a = -10
20

Number = 20
Result = -20
4
Cond is: true
Var1 = 10
Var2 = 1
Now cond is: false
!(a < b) = true
!(a > b) = false
9

Cond is: true
Var1 = 10
Var2 = 1
Now cond is: false
!(a < b) = true
!(a > b) = false
9

Output: 

Cond is: true
Var1 = 10
Var2 = 1
Now cond is: false
!(a < b) = true
!(a > b) = false

 

Operator 3: Increment(++)

It is used to increment the value of an integer. It can be used in two separate ways: 

3.1: Post-increment operator

When placed after the variable name, the value of the operand is incremented but the previous value is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement. 

Syntax: 

num++

Illustration: 

num = 5
num++ = 6

3.2: Pre-increment operator

When placed before the variable name, the operand’s value is incremented instantly.

Syntax: 

++num

Illustration: 

num = 5
++num = 6

Operator 4: Decrement(–)

It is used to decrement the value of an integer. It can be used in two separate ways: 

4.1: Post-decrement operator

When placed after the variable name, the value of the operand is decremented but the previous values is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement. 

Syntax: 

a = -10
0

Illustration: 

a = -10
1

4.2: Pre-decrement operator

When placed before the variable name, the operand’s value is decremented instantly. 

Syntax: 

a = -10
2

Illustration:

a = -10
3

Operator 5: Bitwise Complement(~)

This unary operator returns the one’s complement representation of the input value or operand, i.e, with all bits inverted, which means it makes every 0 to 1, and every 1 to 0. 

Which is a logical operator?

A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. Common logical operators include AND, OR, and NOT.

What are operators and operands?

An operand can be a constant, a variable or a function result. Operators are arithmetic, logical, and relational. As with C, some operators vary in functionality according to the data type of the operands specified in the expression. Arithmetic operators ( +, -, *, /, **, % )

Which operator is used to determine that the operands are not exactly of the same value?

The inequality operator != returns true if its operands aren't equal, false otherwise. For the operands of the built-in types, the expression x != y produces the same result as the expression !(

What type of operators are the following ==?

Relational Operators == (Equal to)– This operator is used to check if both operands are equal.