August 23, 2021, Learn eTutorial

917

In this tutorial, you will travel through various operators used in Java to handle logical, arithmetical, and so and so calculations with the aid of simple and easy examples.

Operators are unique symbols that perform some kind of computation. The objects or values on which operators act are known as operands and the combination of operators and operands are termed as expressions. For instance, 3+6=9 is an expression with 3, 6, and 9 as the operands, and + and = are operators.

Based on the type of operations, java operators are classified into 6 main categories

Arithmetic operators in java are used to perform calculations like addition, subtraction, multiplication, etc on variables and data. The popular operators associated with arithmetic calculations are tabulated below for easy reference.

Operator |
Meaning |
Description |
Example |
---|---|---|---|

+ | Addition | Adds two operands | 10+2=12 |

- | Subtraction | Subtracts right operand from left operand | 10-2=8 |

* | Multiplication | Multiplies two operands | 10*2=20 |

/ | Division | Divides left operand by right operand | 10/2=5 |

% | Modulus | Remainder after division | 10%2=0 |

```
public class OpEx {
public static void main(String[] args) {
//declaring integer variables
int a =10, b= 2;
// + operator for addition
System.out.println("a + b = " + (a + b));
// - operator for subtraction
System.out.println("a - b = " + (a - b));
// * operator for multiplication
System.out.println("a * b = " + (a * b));
// / operator for division
System.out.println("a / b = " + (a / b));
// % operator for division to obtain remainder
System.out.println("a % b = " + (a % b));
}
}
```

**Output:**

a + b = 12 a - b = 8 a * b = 20 a / b = 5 a % b = 0

Relational operators do compare the data provided on the LHS and RHS to give binary outputs i.e. True or False. Here are the six operators demonstrated using the two operands 6 and 9.

Operator | Meaning | Description | Examples |
---|---|---|---|

== | Equal to | Returns True if two operands are equal | 6==9 returns False |

!= | Not Equal to | Returns True if two operands are not equal | 6!=9 returns True |

> | Greater than | Returns True if left operands is greater than the right | 6>9 returns False |

< | Less than | Returns True if left operand is less than the right | 6<9 returns True |

>= | Greater than or equal to | Returns True if left operands is greater than or equal to the right | 6>=9 returns False |

<= | Less than or equal to | Returns True if left operand is less than or equal to the right | 6<=9 returns True |

```
public class OpEx {
public static void main(String[] args) {
// declaring variables
int a = 6, b = 9;
// value of a and b
System.out.println("a = " + a + " and b = " + b);
// == operator
System.out.println(a == b); // 6 == 9 returns false
// != operator
System.out.println(a != b); // 6 != 9 returns true
// > operator
System.out.println(a > b); // 6 > 9 returns false
// < operator
System.out.println(a < b); // 6 < 9 returns true
// >= operator
System.out.println(a >= b); // 6 >= 9 returns false
// <= operator
System.out.println(a <= b); // 6 <= 9 returns true
}
}
```

Logical operators perform binary operations to process data at the machine level ( logic gates like AND, OR, NOR, NAND, etc.). Here is the description of three basic logical operators in java that is extensively used in decision making.

Operator |
Meaning |
Description |
Example |
---|---|---|---|

&& | Logical AND/ Conjunction | Returns True if and only if both statements are true | (2<3) &&(3==3) returns True |

|| | Logical OR / Disjunction | Returns True if any of the statement is true | (2>3) ||(3==3) returns True |

! | Logical NOT/ Negation | Returns true if operand is a negation | !(3==3) returns False |

Truth table of Logical Operators are given below :

Examine the below example to understand the principle of logical operator.

```
public class OpEx {
public static void main(String[] args) {
// && operator
System.out.println((2 > 3) && (3 == 3)); // false
// || operator
System.out.println((2 > 3) || (3 == 3)); // true
// ! operator
System.out.println(!(3 == 3)); // false
}
}
```

Unary operators are those operators that work with only one operand. In the previous section , you have seen the operator for negation(!) which acts only on one operand. Similarly following are the different types of unary operators used in java.

Operator | Meaning | Description | Examples |
---|---|---|---|

+ |
Unary Plus |
Represents a positive number |
+3 |

- |
Unary Minus |
Negates the expression |
-3 |

++ |
Increment Operator |
Increments the value by 1 |
++4 = 4+1 |

-- |
Decrement Operator |
Decrements the value by 1 |
--4 = 4-1 |

! |
Logical Complement operator |
Inverts the value of boolean value |
!T = F |

```
Example: Unary Operator
public class OpEx {
public static void main(String[] args) {
int status = +1;
System.out.println(status); // status is now 1
status--;
System.out.println(status); // After decrement status is now 0
status++;
System.out.println(status); // after increment status is now 1
status = -status;
System.out.println(status); // result is now -1
boolean flag = false;
System.out.println(flag); // false
System.out.println(!flag); // true
}
}
```

These are the most basic and elementary types of operators. The bitwise operator, as its name suggests, manipulates bit-level data. Specifically, the bitwise operators perform operations on integer data at their bit levels.

The bitwise operators are of 7 types:-

The first operator '`&`

'(bitwise AND) is of AND type which copies any bit to the result if the bit exists in both operands. More specifically the existence of a bit is represented with binary `1`

and the counterpart with binary `0`

. Here is a truth table showing the use of the Bitwise AND operator which is much similar to the logical operators.

For example, the Binary representation of **3** is **0000 0011** and that of **7** is **0000 0111**. Let’s see what will be the result of **3 & 7**:

Now let’s have a look how to use Bitwise AND operator in java:

```
public class BAndEx {
public static void main(String[] args) {
int x = 3, y = 7;
System.out.println("x & y = " + (x & y)); // prints 3
}
}
```

Bitwise OR symbolised as ‘I’ functions as a logical OR operator. It replicates a bit(1), if it exists in either or both of the operands.The truth table has demonstrated the working of Bitwise OR operator.

Let’s try to understand how the bitwise OR operator works on two binary numbers 3 and 7.

Below java program shows the operation of Bitwise OR operator.

```
public class BOREx {
public static void main(String[] args) {
int x = 3, y = 7;
System.out.println("x | y = " + (x | y)); // prints 7
}
}
```