Java Operators
Java Operators is a symbol which is used to perform operations. Java Operators has defined such as unary operator, arithmetic operator, relational operator, shift operator, bitwise operator, ternary operator and assignment operator.
Java Arithmetic Operators
Java Arithmetic operators are used to perform common mathematical operations.
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds together two values | x + y |
– | Subtraction | Subtracts one value from another | x – y |
* | Multiplication | Multiplies two values | x * y |
/ | Division | Divides one value from another | x / y |
% | Modulus | Returns the division remainder | x % y |
++ | Increment | Increases the value of a variable by 1 | ++x, x++ |
— | Decrement | Decreases the value of a variable by 1 | –x, x– |
Java Assignment Operators
Java Assignment operators are used to assign values to variables.
Assignment operator (=) to assign the value 5 to a variable called x:
Operator | Example | Same As |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x – 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
&= | x &= 3 | x = x & 3 |
|= | x |= 3 | x = x | 3 |
^= | x ^= 3 | x = x ^ 3 |
>>= | x >>= 3 | x = x >> 3 |
<<= | x <<= 3 | x = x << 3 |
Java Relational Operators
Java Relational operators are used to compare two values:
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 |
Java Logical Operators
Java Logical operators are used to determine the logic between variables or values:
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, returns false if the result is true | !(x < 5 && x < 10) |
Unary Operators
Unary operators are used to perform on single variablle.
Operator | Name | Description | Example |
---|---|---|---|
+ – ! | +ve, -ve, not | It is used with one variable | +i -i ~ ! |
Bitwise Operators
Java Bitwise operators are used to perform the operation on bit level.
Operator | Name | Description | Example |
---|---|---|---|
shift | << >> >>> | Shift bits | c = a << 2; |
bitwise AND | & | & Operation on two variable on binary level | c = a & b; |
bitwise exclusive OR | ^ | XOR operation on two variable on binary level | c = a ^ b; |
bitwise inclusive OR | | | OR operation on two variable on binary level | c = a | b; |
Ternary Operator
Ternary operators is used as conditional and assignment both operations.
Operator | Description | Example |
---|---|---|
<condition>?<var1>:<var2> | ternary | (a<b)?x:y |