Introduction

Java Fundamentals

Java Flow Control

Java Arrays

Object Oriented Programming

# Operators in Java With Examples

Operators are symbols that perform various operations on variables and values in Java. These operations can include arithmetic, relational, bitwise, logical, and assignment tasks. In this tutorial, we will explore the different types of operators available in Java and provide examples of how to use them.

Operators are the symbols that perform different operations on variables and values. For example,
**+** operator is used for the addition of two values, **-** for subtraction and * for multiplication and so on. Operators are the symbols which indicates compiler to perform a specific mathematical or logical operation on some values.

The different types of java operators are,

- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators

### Arithmetic Operators:

Arithmetic operators are used to perform common mathematical operations like addition, subtraction, multiplication, divide, modulus, increment, decrement.

Operator | Name | Description | Example |
---|---|---|---|

+ | Addition | Adds two values together | a + b |

- | Subtraction | Subtracts one value from another | a - b |

* | Multiplication | Multiplies two values | a * b |

/ | Division | Divides one value by another | a / b |

% | Modulus | Returns the division remainder | a % b |

Here is an example of how to use arithmetic operators in Java:

```
// Basic java program to perform Arithmetic operations."
import java.io.*;
class HashArithmetic {
public static void main(String[] args) {
// declare variables a and b.
int a = 9, b = 4;
// Addition Operator(+)
System.out.println("a + b = " + (a + b));
// Subtraction Operator(-)
System.out.println("a - b = " + (a - b));
// Multiplication Operator(*)
System.out.println("a * b = " + (a * b));
// Division Operator(/)
System.out.println("a / b = " + (a / b));
// Modulo Operator(%)
System.out.println("a % b = " + (a % b));
}
}
```

Output :

a + b = 13 a - b = 5 a * b = 36 a / b = 2 a % b = 1

As we might have predicted, the operators +, -, and * compute addition, subtraction, and multiplication, respectively.

In basic math, **9/4** **=** **2.25**. However, the output of the programme is **2**.

It is because **a** and **b** are both integer variables. The output is therefore also an integer. The compiler displays result **2** rather than **2.25** because it ignores the term after the decimal point.

The remainder is calculated using the modulo operator%. The remainder is **1** when **a=9** is divided by **b=4**. Only integers can be used with the **%** operator.

### Relational Operators:

Relational operators are used to check relations between two values like Is Equal to, Greater than, Less than, Greater than or Equal to, Less than or Equal to, Not Equal to.

Operator | Name | Example |
---|---|---|

== | Equal to | 4 == 3 is evaluated to 0 |

> | Greater than | 4 > 3 is evaluated to 1 |

< | Less than | 4 < 3 is evaluated to 0 |

!= | Not equal | 4 != 3 is evaluated to 1 |

>= | Greater than or equal to | 4 >= 3 is evaluated to 1 |

<= | Less than or equal to | 4 <= 3 is evaluated to 0 |

##### Example for Relational Operators in Java

```
#include <stdio.h>
int main()
{
int a = 10, b = 10, c = 20;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
```

Output :

10 == 10 is 1 10 == 20 is 0 10 > 10 is 0 10 > 20 is 0 10 < 10 is 0 10 < 20 is 1 10 != 10 is 0 10 != 20 is 1 10 >= 10 is 1 10 >= 20 is 0 10 <= 10 is 1 10 <= 20 is 1

### Bitwise Operators:

Bitwise operators in Java are used to perform operations on individual bits. They can be used with any of the integer types. It works on bit-by-bit operations.

Operators | Name |
---|---|

& | Bitwise AND |

| | Bitwise OR |

^ | Bitwise exclusive OR |

~ | Bitwise complement |

<< | Shift left |

<< | Shift right |

##### Example for Bitwise Operators in Java

```
// Basic java program to perform Bitwise operations.
import java.io.*;
class HashArithmetic {
public static void main(String[] args) {
// declare variable a.
int a = 60; //Binary representation = 0 0 1 1 1 1 0 0
// left shift( << )
System.out.println(a << 2);
// right shift( >> )
System.out.println(a >> 2);
}
}
```

Output :

240 15

### Logical Operators:

A logical operator expression returns either 0 or 1, depending on whether the expression is true or false. In java programming, logical operators are frequently used in decision making.

Operator | Name | Example |
---|---|---|

&& | Logical AND | True only if all operands are true |

|| | Logical OR | True only if either one operand is true |

! | Logical NOT | True only if the operand is 0 |

##### Example for Logical Operators in Java

```
// Basic java program to perform Logical operations."
import java.io.*;
class HashLogical {
public static void main(String[] args) {
int x = 5;
// && Operator
System.out.println(x > 4 && x < 8);
System.out.println(x > 4 && x < 3);
// || Operator
System.out.println(x > 4 || x < 8);
System.out.println(x > 6 || x < 3);
// ! Operator
System.out.println(!(x > 4 && x < 8));
System.out.println(!(x > 4 && x < 3));
}
}
```

Output :

true false true false false true

### Assignment Operators:

Assignment operators assign values to variables. It is also used to assign the value on its right to the operand on its left.

Operators | Example | Equivalent expression |
---|---|---|

+= | a += b | a = a+b |

-= | a -= b | a = a-b |

*= | a *= b | a = a*b |

/= | a /= b | a = a/b |

%= | a %= b | a = a%b |

##### Example for Assignment Operators in Java:

```
// Basic java program to perform Logical operations."
// Basic java program to perform Assignment operations."
import java.io.*;
class HashLogical {
public static void main(String[] args) {
int a = 20;
int b = 15;
int c = 5;
int d = 50;
a += 5; //a = a + 5
b -= 5; //b = b - 5
c *= 5; //c = c * 5
d /= 5; //d = d / 5
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
```

Output :

25 10 25 10