### Java Operators

Java Operators are symbols that perform operations on variables and values. Java provides many types of operators which can be used according to our usage. They are classified into following types:

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

**Arithmetic Operators**

Arithmetic operators are used to perform mathematic operations on variables and data. They operate on numeric values. In simple words they can be used to perform the mathematical operations of addition, subtraction, multiplication, and division.

Operator | Description |

+ | Performs addition |

– | Performs subtraction |

* | Performs multiplication |

/ | Performs division |

% | Returns the remainder of the division |

**Sample Code:**

```
class Arithmetic {
public static void main(String[] args) {
// variables
int a = 10, b = 5;
// Print sum using addition operator
System.out.println("a + b = " + (a + b));
// Print difference of a and b using subtraction operator
System.out.println("a - b = " + (a - b));
// Print product using multiplication operator
System.out.println("a * b = " + (a * b));
// Print result of division
System.out.println("a / b = " + (a / b));
}
}
```

**Assignment Operators**

The assignment operator is a special operator that assigns a value to a variable. It is represented by the “=” sign.

**Example**

```
String name;
name = "Daniel Johnson"
```

In above expression, = is the assignment operator. It assigns the value "Daniel Johnson" on its right to the variable name on its left.

Operator | Description |

+= | Combines addition with the assignment. So a += 10 is the same as a=a+10 |

-= | Combines subtraction with the assignment. So a -= 10 is the same as a=a-10 |

*= | Combines multiplication with the assignment. So a *= 10 is the same as a=a*10 |

/= | Combines division with the assignment. So a /= 10 is the same as a=a/10 |

%= | Combines modulus with the assignment. So a %= 10 is the same as a=a%10 |

**Sample Code:**

```
class AssignmentOperator {
public static void main(String[] args)
{
// Declare variable
int number;
// Assign value
number = 5;
// Or declare and assign value in same step
String name = "John";
// Print the assigned values
System.out.println("number is assigned: " + number);
System.out.println("name is assigned: " + name);
}
}
```

**Relational Operators**

Relational Operators are used to compare/check relation between two values. They always return a boolean value which is either true or false, by comparing the two values.

Operator | Description |

< | Performs Less Than Comparison |

<= | Performs Less Than or Equal To Comparison |

> | Performs Greater Than Comparison |

>= | Performs Greater Than or Equal To Comparison |

== | Performs Equal To Comparison |

!= | Performs Not Equal To Comparison |

**Sample Code:**

```
class RelationalOperators {
public static void main(String[] args) {
// Declare variables
int a = 10, b = 5;
// Print value of a and b
System.out.println("a is " + a + " and b is " + b);
// Equality operator (==) Check if a equals to b or not
System.out.println(a == b);
// Not Equal Operator (!=)
System.out.println(a != b);
// Greater than (>) operator
System.out.println(a > b);
// Less than (<) operator
System.out.println(a < b);
// Greater than or equals to (>=) operator
System.out.println(a >= b);
// Less than or equals to (<=) operator
System.out.println(a <= b);
}
}
```

**Logical Operators**

Logical operators are very important in decision making. They are used to check whether a condition(IF/ELSE) is true or false. They operate on boolean values and return a boolean. These operators are used to perform logical “AND”, “OR” and “NOT” operation.

**Example:**

```
int a = 5;
int b = 10;
boolean condition1 = a < b;
boolean condition2 = b > a;
if condition1 && condition2:
System.out.println("Both condition1 and condition2 are true");
```

Operator | Description |

|| (OR) | Returns true if any one of its operands are true, otherwise returns false |

&& (AND) | Returns true if both its operands are true, otherwise returns false |

! (NOT) | It is a unary operation. It returns the inverse of the operand. |

**Sample Code:**

```
class LogicalOperator {
public static void main(String[] args) {
// AND (&&) operator
System.out.println((10 > 5) && (8 < 10));
System.out.println((10 > 5) && (8 > 10));
// OR (||) operator
System.out.println((10 < 5) || (8 > 5));
System.out.println((10 < 3) || (8 < 5));
// NOT (!) operator
System.out.println(!(10 == 5));
System.out.println(!(10 > 5));
}
}
```

**Unary Operators**

Unary operators in Java are used with only one operand. They are used for increment, decrement and negation. For e.g., ++ is a unary operator that increases the value of a variable by 1. It means ++10 will return 11.

Operator | Description |

++ | Increments its operand by 1 |

— | Decrements its operand by 1 |

There are two types of increment/decrement operators, pre and post. Let's understand it from below example:

**Sample Code:**

```
class UnaryOperator {
public static void main(String[] args)
{
// Initialize variable
int num = 10;
// First 10 gets printed as it's a post-increment operator
// Then it get incremented to 11
System.out.println("Post " + "increment = " + num++);
// Now num is 11 and get incremented to 12, as it's a pre-increment operator
// Then 12 will be printed
System.out.println("Pre " + "increment = " + ++num);
}
}
```

Same is applied to decrement operator but in reverse.

**Bitwise Operators**

Java provides several bitwise operators that are only applied to the integer types: long, int, short and char. These operators act upon the individual bits of their operands. These are mostly not used in Java rather they are mostly used in microprocessor or electronic circuits etc.

Operator | Description |

~ | Bitwise Complement |

<< | Left Shift |

>> | Right Shift |

>>> | Unsigned Right Shift |

& | Bitwise AND |

^ | Bitwise exclusive OR |

###
__Follow Us__

__Follow Us__

**Quora**

*Space*

**|**

*Page*

**|**

**Telegram**

*Channel*

*Feel free to ask queries or share your thoughts in comments or email us.*

## Comments

## Post a Comment