Variables are available in Java, which are the important aspects of code. To store the values, reserved memory locations are needed which are called Variables. To manipulate the variables, we have the operators available in Java. There are different types of operator in Java such as Arithmetic Operators, Bitwise Operators, Logical Operators, and Some other Miscellaneous Operators. Today let us get to know about some of the mostly used and required operators in Java.
The Arithmetic Operators:
As we use algebra in mathematical expressions, we use Arithmetic operators in the same way for calculations. Arithmetic operators constitute of the following list.
For example, let us assume that the variable A and B hold the values 20 and 30 respectively, now we check the operators on them.
1 |
Addition (+) This adds the values of variables on both the sides of the operator. For ExampleÂ A + B will give 50 |
2 |
Subtraction (-) Left hand operand is subtracted from the right-hand operand For ExampleÂ A – B will give -10 |
3 |
Multiplication (*) Values on both the sides of the operator are multiplied For ExampleÂ A * B will give 600 |
4 |
Division (/) Left-hand and the right-hand operands are divided For ExampleÂ B / A will give 1.5 |
5 |
Modulus (%) The remainder is resulted when left-hand operand is divided by a right-hand operand. For ExampleÂ B % A will give 1 |
6 |
IncrementÂ Value of the operand is increased by 1. For ExampleÂ B++ gives 31 |
7 |
Decrement (– ) Value of the operand is decreased by 1. For ExampleÂ B– gives 29 |
The Relational Operators:
To compare two numbers in Java is one by the relational operators and they return a Boolean value which gives a true or false value. Following are the relational operators exists in Java language
Let us have an example where variable A and B has the values 10 and 20 respectively, then
1 |
== (equal to) It is used to check the values of two given operands are same or not, if same it returns true value otherwise false. ExampleÂ (A == B) is false in this case. |
2 |
!= (not equal to) It is used to check the values of two given operands are same or not, if same it returns false value otherwise true. ExampleÂ the result of A != B is true. |
3 |
> (greater than) The result of this condition becomes true if the left operandâ€™s value is greater than the value of the right operand. ExampleÂ (A > B) is not true. |
4 |
< (less than) The result of this condition becomes true if the left operandâ€™s value is less than the value of the right operand. Example result ofÂ A < B is true. |
5 |
>= (greater than or equal to) The result of this condition becomes true if the left operandâ€™s value is greater than or even equal to the value of the right operand. ExampleÂ (A >= B) is not true. In the same way, there is also <= less than or equal to. |