Operators in Java
In the last blog, we learned about the Variables in Java. If you want to learn more about variables visit Variables in Java. In this blog, we will go through the Operators in Java and the types of Operators we use for performing the operations. In general, operators are used for performing some calculations. The same is applicable in Java, operators are used for performing operations on variables like addition, subtraction, etc. There are the following types of operators:
- Arithmetic Operators(+, -, *, /)
- Relational Operators(== , >, <,>=, <=, !=)
- Conditional Operators(&&, ||)
- Assignment Operators(=, +=, -=, *=, /=, %=, &=, ^=, |=, >>=, <<=, >>>=)
- Ternary Operators(?, :)
- Unary Operators(+, -, ++, --, ~, !)
- Bitwise Operators(&, |, ^, ~, <<, >>, >>>)
Arithmetic Operators
The operators that perform arithmetic operations like addition, subtraction, division, multiplication, etc. are called arithmetic operators. ‘+’ is used for addition, ‘-’ operator is used for subtraction of the two numbers. The ‘*’ operator is used for multiplication. The division operator(/) is used for integer division when having both the arguments as integers and also floating point division can be performed with the help of it. The remainder is denoted by %. If we try to use 0 for the division of integers i.e. 10/0, then it will give us an exception, and floating point division will give us an infinite result or NaN result.
Arithmetic Operators | Description |
---|---|
+ | Performs addition i.e. a+b |
- | Performs subtraction i.e. a-b |
* | Performs multiplication i.e. a*b |
/ | Performs division i.e. a/b |
% | Returns the remainder of the division which is performed a%b (Modulo Operation) |
e.g.
Output:
Relational Operators
Relational Operators are used for checking the relationship between the two operands. "==" operator is used for checking an equality. It returns the result in a boolean i.e. either true or false.
e.g.
Output:
In the above example a==b will return true value. In order to check inequality we use the != operator.
e.g.
Output:
Here it will check whether a is not equal to b which is true, so it will return true.
In order to check which number is smaller than / greater than / equal to / greater than equal / smaller than equal amongst the two, we use the "<", ">", "==", "<=", ">=" operator.
e.g.
Output:
Relational Operators | Description |
---|---|
== | Is Equal to e.g. if(10 == 10) condition will be true |
< | Less than e.g. if(9 < 10) condition will be true |
> | Greater Than e.g. if(8 > 7) condition will be true |
<= | Less Than or Equal To e.g. if(20 <= 40) condition will be true |
>= | Greater Than or Equal To e.g. if(10 >= 10) condition will be true |
!= | Not Equal To e.g if(10 != 10) condition will be false |
e.g.
e.g.
Conditional Operators
Logical Operators are used to perform conditional operations like Conditional AND, OR, NOT on two boolean expressions. AND operator returns true if and only if both the conditions satisfy, otherwise it returns false. OR operator returns true even if one of the two conditions satisfies.
Logical Operators | Description |
---|---|
&& | Conditional AND |
|| | Conditional OR |
e.g.
Output:
Assignment Operator
Assignment operator is the most commonly used operator. It is mostly used for assigning the values to the variables. On the left side of the = operator we write a variable name followed by the operator i.e. = and then on the right side, we assign the values to that variable. We can also combine two operators i.e. arithmetic operator and assignment operator which will give us compound assignments for example +=, -=, *=, /=, %=, &=, ^=, |=, <<=,>>=, >>>=, etc.
e.g.
Output:
Ternary Operators
Ternary Operator is the only operator which accepts three operators. It is the replacement for if else statement and is used in Java mostly. It makes the code easy, and short because the statement ends in one line only.
Syntax:
Here if the condition will be true then it will execute the first expression i.e. expression1, but if the condition fails then it executes the second expression i.e. expression2.
e.g.
Output:
Unary Operators
Unary operator performs the operation on one operand only. It can perform the increment, decrement operation on the value by one, can invert the values of the boolean, etc. Unary Operators are : +, -, ++, --, ~, !.
e.g.
Output: