Java Operators
In an attempt to advance our learning, we will talk about Java operators today.
Operators In Java
Java operators are used for performing operations on variables and values.
Two values are added together using the + operator in the example below:
Example: 
Example: 
The + operator is often used to add two values together, like in the example above, but it can also add values, or variables, together:
Example: 
Example: 
Operators in Java are divided into the following groups:
- Arithmetic operators.
- Assignment operators.
- Bitwise operators. (In Java, bitwise operators are rarely used.)
- Comparison operators.
- Logical operators.
Java Arithmetic Operators
When it comes to Java operators, arithmetic operators are used to perform common mathematical operations.
Operator | Name | Overview | Example | Try it |
---|---|---|---|---|
+ | Addition | Sums the two values. | x + y | Execute |
– | Subtraction | Calculate the difference between two values. | x – y | Execute |
* | Multiplication | Two values are multiplied. | x * y | Execute |
/ | Division | Calculates the ratio of two values. | x / y | Execute |
% | Modulus | Returns the remainder after division | x % y | Execute |
++ | Increment | 1 is added to a variable’s value | ++x | Execute |
— | Decrement | A variable’s value is decreased by one | –x | Execute |
Java Assignment Operators
In order to assign values to variables, assignment operators are used.
The following example uses the assignment operator (=) to assign 30 to a variable mrx:
Example: 
Example: 
Addition assignment (+=) adds a value to a variable:
Example: 
Example: 
Here is a list of all assignment operators:
Operator | Example | Same As | Try it |
---|---|---|---|
= | mrx = 5 | mrx = 5 | Execute |
+= | mrx += 3 | mrx = mrx + 3 | Execute |
-= | mrx -= 3 | mrx = mrx – 3 | Execute |
*= | mrx *= 3 | mrx = mrx * 3 | Execute |
/= | mrx /= 3 | mrx = mrx / 3 | Execute |
%= | mrx %= 3 | mrx = mrx % 3 | Execute |
&= | mrx &= 3 | mrx = mrx & 3 | Execute |
|= | mrx |= 3 | mrx = mrx | 3 | Execute |
^= | mrx ^= 3 | mrx = mrx ^ 3 | Execute |
>>= | mrx >>= 3 | mrx = mrx >> 3 | Execute |
<<= | mrx <<= 3 | mrx = mrx << 3 | Execute |
Java Bitwise Operators
Using Java’s bitwise operators, you can perform operations on individual bits.
In Java, there are several bitwise operators:
Operator | Overview |
---|---|
~ | Bitwise Components |
<< | Left Shift |
>> | Right Shift |
>>> | Unsigned Right Shift |
& | Bitwise AND |
^ | Bitwise exclusive OR |
Java Comparison Operators
In Java operators, two values can be compared by using comparison operators:
Operator | Name | Example | Try it |
---|---|---|---|
== | Equal to | mrx == mrexamples | Execute |
!= | Not equal | mrx != mrexamples | Execute |
> | Greater than | mrx > mrexamples | Execute |
< | Less than | mrx < mrexamples | Execute |
>= | Greater than or equal to | mrx >= mrexamples | Execute |
<= | Less than or equal to | mrx <= mrexamples | Execute |
Java Logical Operators
A logical operator determines the logic between variables or values in Java Operators:
Operator | Name | Overview | Example | Try it |
---|---|---|---|---|
&& | Logical and | If both statements are true, then true is returned. | mrx < 5 && mrx < 10 | Execute |
|| | Logical or | If one of the statements is true, then true is returned. | mrx < 5 || mrx < 4 | Execute |
! | Logical not | Returns false if the result is true and vice versa | !(mrx < 5 && mrx < 10) | Execute |