Java Operators
Operators perform operations on operands like variables or values.
In Java, there are five types of operators based on the type of operations they perform. They are
- Java Arithmetic Operators
- Java Assignment Operators
- Java Relational Operators
- Java Logical Operators
- Java Bitwise Operators
Arithmetic Operators
Java Arithmetic Operators perform mathematical arithmetic operations and return the result. Following are the arithmetic operators supported in Java.
Arithmetic Operation | Operator Symbol | Example | Description |
---|---|---|---|
Addition | + | a + b | Returns sum of a and b. |
Subtraction | - | a – b | Returns difference of b from a. |
Multiplication | * | a * b | Returns product of a and b. |
Division | / | a / b | Returns the quotient when a is divided by b. |
Modular Division | % | a % b | Returns reminder when a is divided by b. |
Increment | ++ | a++ is Post-Increment.++a is Pre-Increment. | Increments the value of a by one. Post-Increment: The value of a is updated, only after the execution moves to next statement.Pre-Increment: The value of a is updated during the execution of this statement itself. |
Decrement | -- | a-- is Post-Decrement.--a is Pre-Decrement. | Decrements the value of a by one. Post-Decrement: The value of a is updated, only after the execution moves to next statement.Pre-Decrement: The value of a is updated during the execution of this statement itself. |
Arithmetic Operators Tutorials
The following tutorials provide a well detailed explanation and examples for each of the Arithmetic Operators in Java.
- Java Addition Operator
- Java Subtraction Operator
- Java Multiplication Operator
- Java Division Operator
- Java Modulus Operator
- Java Increment Operator
- Java Decrement Operator
Java Assignment Operators
Java Assignment Operators are used to optionally perform an action with given operands and assign the result back to given variable (left operand).
The following table specifies symbol, example, and description for each of the Assignment Operator in Java.
Assignment Operation | Operator Symbol | Example | Description |
---|---|---|---|
Simple Assignment | = | x = 2 | Assign x with 2. |
Addition Assignment | += | x += 3 | Add 3 to the value of x and assign the result to x. |
Subtraction Assignment | -= | x -= 3 | Subtract 3 from x and assign the result to x. |
Multiplication Assignment | *= | x *= 3 | Multiply x with 3 and assign the result to x. |
Division Assignment | /= | x /= 3 | Divide x with 3 and assign the quotient to x. |
Remainder Assignment | %= | x %= 3 | Divide x with 3 and assign the remainder to x. |
Bitwise AND Assignment | &= | x &= 3 | Perform x & 3 and assign the result to x. |
Bitwise OR Assignment | |= | x |= 3 | Perform x | 3 and assign the result to x. |
Bitwise-exclusive-OR Assignment | ^= | x ^= 3 | Perform x ^ 3 and assign the result to x. |
Left-shift Assignment | <<= | x <<= 3 | Left-shift the value of x by 3 places and assign the result to x. |
Right-shift Assignment | >>= | x >>= 3 | Right-shift the value of x by 3 places and assign the result to x. |
Assignment Operators Tutorials
The following tutorials provide a well detailed explanation and examples for each of the Assignment Operators in Java.
- Java Simple Assignment (=) Operator
- Java Addition Assignment Operator
- Java Subtraction Assignment Operator
- Java Multiplication Assignment Operator
- Java Division Assignment Operator
- Java Remainder Assignment Operator
- Java Bitwise AND Assignment Operator
- Java Bitwise OR Assignment Operator
- Java Bitwise XOR Assignment Operator
- Java Left-shift Assignment Operator
- Java Right-shift Assignment Operator
Relational Operators
Relational Operators are used to compare two operands if they are equal, greater, lesser, etc.
The following table gives more information about these relational operators in java.
Relational Operation | Operator Symbol | Example | Description |
---|---|---|---|
Equal to | == | a == b | Returns true if a is equal to b , else false. |
Not equal to | != | a != b | Returns true if a is not equal to b , else false. |
Greater than | > | a > b | Returns true if a is greater than b , else false. |
Less than | < | a < b | Returns true if a is less than b , else false. |
Greater than or equal to | >= | a >= b | Returns true if a is greater than or equal to b , else false. |
Less than or equal to | <= | a <= b | Returns true if a is less than or equal to b , else false. |
Relational Operators Tutorials
The following tutorials provide a well detailed explanation and examples for each of the Relational Operators in Java.
- Java Equal To Operator
- Java Not Equal Operator
- Java Greater Than Operator
- Java Less Than Operator
- Java Greater Than or Equal To Operator
- Java Less Than or Equal To Operator
Logical Operators
Logical Operators are used to create boolean conditions, modify a boolean expression, or combine two or more simple conditions to form a complex condition.
Java supports the following Logical Operators.
Logical Operation | Operator Symbol | Example | Description |
---|---|---|---|
AND | && | a && b | Returns logical AND of a and b . |
OR | || | a || b | Returns logical OR of a and b . |
NOT | ! | !a | Returns logical Not of a . |
Logical Operators Tutorials
The following Java tutorials cover each of these Logical Operators in detail.
Bitwise Operators
Java Bitwise Operators are used to perform bitwise operations on integer or char operands. Bitwise operations are done at bit level, meaning, operations like AND, OR, XOR, etc., are done between respective bits of the operands.
The following table specifies symbol, example, and description for each of the Assignment Operator in Java.
Bitwise Operation | Operator Symbol | Example | Description |
---|---|---|---|
AND | & | x & y | Returns the bitwise AND operation between x and y. |
OR | | | x | y | Returns the bitwise OR operation between x and y. |
XOR | ^ | x ^ y | Returns the bitwise XOR operation between x and y. |
Complement | ~ | ~x | Returns the complement of x. |
Left Shift | << | x << y | Returns the result of x left shifted by y number of places. |
Right Shift | >> | x >> y | Returns the result of x right shifted by y number of places. |
Bitwise Operators Tutorials
The following Java tutorials cover each of these Bitwise Operators in detail.
- Java Bitwise AND Operator
- Java Bitwise OR Operator
- Java Bitwise XOR Operator
- Java Bitwise Complement Operator
- Java Bitwise Left-shift Operator
- Java Bitwise Right-shift Operator
Conclusion
In this Java Tutorial, we learned about different operators in Java, with separate tutorial for each of the operator.