Java Operators

Operators in Java
The operator is the symbol that tells the compiler to perform some operation over the expression.
Example: 'sum = a + b' where a and b are the operands whereas + is the operator.
There are different types of operators in Java. Let's discuss them one by one.
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Relational operators
Bitwise operators
Arithmetic operators
They are further divided into two parts: Unary and Binary operators.
Unary operators are those which are performed on the number "1". They are ++ and -- whereas binary operators are those which are performed on the two operands. They are +, -, *, /, %.
So arithmetic operators are:
| Operator | Name | Description | Example |
| + | Addition | Adds two values | 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 | Gives the division remainder | A % B |
| ++ | Increment | Increases the value of a variable by 1 | A++ |
| -- | Decrement | Decreases the value of a variable by 1 | A-- |
Increment and decrement operators are of two types:
Pre-increment and pre-decrement
Post-increment and post-decrement
Assignment Operators
Assignment operators are used to assign values to variables.
Some of the assignment operators are:
| Operator | Example | Same As |
| \= | a = 3 | a = 3 |
| += | a += 3 | a = a + 3 |
| -= | a -= 3 | a = a - 3 |
| *= | a *= 3 | a = a * 3 |
| /= | a /= 3 | a = a / 3 |
| %= | a %= 3 | a = a % 3 |
Comparison Operators
Comparison operators are used to compare two values (or variables). It is important because it helps us to find answers and make decisions.
The return value of a comparison is either true or false. These values are known as Boolean values. Some of the comparison operators are given as follows:
| Operator | Name | Example |
| \== | Equal to | x == y |
| != | Not equal | x != y |
| \> | Greater than | x > y |
| < | Less than | x < y |
| \>= | Greater than or equal to | x >= y |
| <= | Less than or equal to | x <= y |
Logical Operators
Logical operators are used to determine the logic between variables or values. Some of the logical operators are:
| Operator | Name | Description | Example | ||||
| && | Logical AND | Returns true if both statements are true | x < 5 && x < 10 | ||||
| Logical OR | Returns true if one of the statements is true | x < 5 | x < 4 | ||||
| ! | Logical NOT | Reverse the result, return false if the result is true | !(x < 5 && x < 10) |
We will learn more about the operators in the coming lessons. For now, just remember these operators.


