
Java Operators performs some operations on the variables and value like addition, subtraction, division etc.
In the programming language, operators are special symbols that perform specific operation on operands and then returns a result.
If an operator acts on a single variable then it is called unary operator, if operator acts on two variables it is called binary operator and operator acts on three variables then it is called ternary operator.
Table of Contents
Groups of Java Operators
There are following groups of java operators:
- Arithmetic Operators
- Assignment Operators
- Bitwise and Bit Shift Operators
- Conditional Operators
- Equality and Relational Operators
- Type Comparison Operator
- Unary Operators
1. Arithmetic Operators
We use the arithmetic operators to perform addition, subtraction, multiplication, division and modulus.
# | Name | Operator | Example | Details |
---|---|---|---|---|
1 | Addition | + | a + b | Adds two values |
2 | Subtraction | – | a – b | Subtracts two values |
3 | Multiplication | * | a * b | Multiplies one by another value |
4 | Division | / | a / b | Divides one by another |
5 | Modulus | % | a % b | Returns the division remainder |
2. Assignment Operators
We assign value to variables using assignment operators. In other words, we assign the value to the right of the operand to the left of it.
# | Operator | Example |
---|---|---|
1 | = | z= x + y will assign value of x + y into z |
2 | += | x += 10 is equivalent to x = x + 10 |
3 | -= | x -= 10 is equivalent to x = x – 10 |
4 | *= | x *= 10 is equivalent to x = x * 10 |
5 | /= | x /= 10 is equivalent to x = x / 10 |
6 | %= | x %= 10 is equivalent to x = x % 10 |
7 | &= | x &= 10 is equivalent to x = x & 10 |
8 | |= | x |= 10 is equivalent to x = x | 10 |
9 | ^= | x ^= 10 is equivalent to x = x ^ 10 |
10 | <<= | x <<= 10 is equivalent to x = x << 10 |
11 | >>= | x >>= 10 is equivalent to x = x >> 10 |
3. Bitwise and Bit Shift Operators
These operators act on bits 0 and 1 of the operands and act on byte, short, int and long.
# | Operator | Name |
---|---|---|
1 | ~ | Unary bitwise complement |
2 | << | Signed left shift |
3 | >> | Signed right shift |
4 | >>> | Unsigned right shift |
5 | & | Bitwise AND |
6 | ^ | Bitwise exclusive OR |
7 | | | Bitwise inclusive OR |
4. Conditional Operators
# | Operator | Name |
---|---|---|
1 | && | Conditional AND |
2 | || | Conditional OR |
3 | ?: | Ternary |
5. Equality and Relational Operators
The equality and relational operations check whether one operand is greater than, less than, equal to, or not equal to other operand.
# | Operator | Name |
---|---|---|
1 | == | equal to |
2 | != | not equal to |
3 | > | greater than |
4 | >= | greater than or equal to |
5 | < | less than |
6 | <= | less than or equal to |
6. Type Comparison Operator
This operator checks whether the given type is compatible with another type. In other words, whether specified the instance is of a specified type(interface, class, subclass).
# | Operator | Name |
---|---|---|
1 | instanceof | Compares an object to a specified type |
7. Unary Operators
# | Operator | Name |
---|---|---|
1 | + | Indicates positive value (Unary plus operator) |
2 | – | Negates an expression (Unary minus operator) |
3 | ++ | Increments a value by 1 (Increment operator) |
4 | — | Decrements a value by 1 (Decrement operator) |
5 | ! | Inverts the value of a boolean (Logical complement operator) |
Example of Arithmetic Java Operators
In addition to the explanation here is an example of arithmetic operators that shows how to perform operations using these types of operators.
public class ArithmeticOperations {
public static void main(String[] args) {
int result = 4 + 2;
// result is now 6
System.out.println("4 + 2 = " + result);
int originalResult = result;
result = result - 1;
// result is now 5
System.out.println(originalResult + " - 1 = " + result);
originalResult = result;
result = result * 2;
// result is now 10
System.out.println(originalResult + " * 2 = " + result);
originalResult = result;
result = result / 2;
// result is now 5
System.out.println(originalResult + " / 2 = " + result);
originalResult = result;
result = result + 8;
// result is now 13
System.out.println(originalResult + " + 8 = " + result);
originalResult = result;
result = result % 7;
// result is now 6
System.out.println(originalResult + " % 7 = " + result);
}
}
Java Operators Precedence
When several operators are used in a statement then it is important to know which operator will execute first and which will come next. Operators with higher priority are evaluated before operators with lower priority. But sometimes operators with similar expressions may appear in the same expression.
Rules of operator precedence for Java Operators
To determine this, here are some rules of operator preference:
- Firstly the contents inside () and [] braces will be executed.
- ++ and — will come afterwards
- And then *, / and % will execute.
- Next, + and – will come
- Then Relational operators
- Then Boolean an bitwise operators
- Logical operators will come afterwards
- Then ternary operator.
- At last, assignment operator are executed.
Operators | Precedence |
---|---|
postfix | expr++ expr-- |
unary | ++expr --expr +expr -expr ~ ! |
multiplicative | * / % |
additive | + - |
shift | << >> >>> |
relational | < > <= >= instanceof |
equality | == != |
bitwise AND | & |
bitwise exclusive OR | ^ |
bitwise inclusive OR | | |
logical AND | && |
logical OR | || |
ternary | ? : |
assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
In Java Assignment operators are evaluated from right to left and the binary operators are evaluated from left to right.
In conclusion, you learned that operators make programming easier by helping the programmer to perform any operation by simply mentioning the symbol.