Operators in Java
Video Tutorial
Overview
Operators can be easily defined as characters that represent an operation. These symbols perform different operations on several variables and values. There are a total of 8 types of operators in Java: Unary, Arithmetic, Shift, Relational, Bitwise, Logical, Ternary, and Assignment operators.
Introduction to Operators in Java
Operators are the formula that can perform an operation involving the values of the operands. Let's talk about an example 5 + 6 = 11. Here, 5 and 6 are the operands, and + is called the operator. We can also use operators for a single operand as well as more than two operands in some situations. Here we are going to be talking about Java operators.
Now let’s find out about the types of operators in Java.
Types of Operators in Java
The list given below informs us about the entire range of operators that are provided to us by the Java programming language.
- Unary Operator
- Relational Operator
- Assignment Operator
- Java instanceof Operator
- Ternary Operator or Conditional Operators
- Arithmetic Operator
- Logical Operator
- Bitwise Operator
Let’s look more deeply into each one!
1. Unary Operators
Unary Operators, as the name suggests, need only a single operand, and as they are operators, they are used to increment a value, decrement, or eliminate it.
Different kinds of unary operators are:
Operator | Name | Description |
---|---|---|
++ | Increment Operator | Increments the value of the operand by 1. This operator can be used as a post-increment and as a pre-increment. Post increment is used when we want to use the current value and then perform the incrementation. On the other hand, pre-increment is used when we perform the incrementation first and then use the value. |
-- | Decrement Operator | Decrements the value of the operand by 1. (opposite of increment operator) As we've seen in the above increment operator, this operator also has two use cases, the first being post-decrement and the second as pre-increment having similar behavior as explained above. |
+ | Unary Plus Operator | Presents the positive value. It can perform an automated conversion to int when the other types are byte, char, and short. |
- | Unary Minus Operator | Normally used for eliminating or negating values. |
! | Logical not Operator | It is used for inversion of the boolean value. This operator reverses the value of a boolean expression. Let's say a boolean value is true and if we use the! operator with a true expression, it will give us the result as false. So it inverses the boolean value and returns the result. |
Unary Operators in Java are used to either increment or decrement the value by one. Let's see an example.
Example
Output
Explanation:
- In the above program, we have incremented the value of sum by 1. Hence when we display the value, the result on the output is 1.
- Then, we performed a post-decrement, and the value is printed after the decrement; hence, the updated value that is 0 is displayed on the output screen.
- Again, we have used post increment, so the value now is 1. We then performed the unary minus operator on sum, which displayed the negation of the value 1. Therefore the value now is -1.
- We then show the impact of logical not operator by performing this operation on the result variable.
2. Relational Operator
Operators that are used for checking the relations like greater than, less than, equal to, or not equal between the two operands are known as Relational Operators. It returns the result as a boolean value which later on, after comparison and is substantially used in the process of looping and conditional if-else statements.
Symbol | Name | Function |
---|---|---|
< | Less than | The value returned will be true if the RHS value is greater than the LHS value. |
> | Greater than | The value that is returned will be true if the LHS value is greater than the RHS value. |
== | Equal To | The value returned will be true if the LHS value is equal to the RHS value. |
!= | Not Equal To | The value returned will be true if the LHS value is not equal to the RHS value |
<= | Less than or Equal To | The value returned will be true if the LHS value is less than or equal to the RHS value. |
>= | Greater than or Equal To | The value returned will be true if the LHS value is greater than or equal to the RHS value. |
These operators can be used to compare two operands, for example, we can use relational operators in Java to compare the age of two persons, the price of two items, etc.
Example
Output
3. Assignment Operator
As the name suggests assignment operator assigns a value to any variable. It uses the right to left associativity. This means that the value given on the RHS of the assignment operator is given to the variable on the LHS, which leads to the RHS being calculated before being able to use it.
The assignment operators can be used as Compound Assignment Operators, which provide a shorter syntax for assessing the result of an arithmetic operator. Let's see some examples.
Operator | Example | Equivalent to |
---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
Assignment Operators in Java are used for modifying a value to perform some mathematical calculations and for assigning values to the operands.
Example:
Output
4. Java instanceof Operator
The Instanceof Operators in Java will crosscheck if an object is of a certain class or not. The instanceof operator works on is-a relationship. It is also used for checking the data type of a variable.
Syntax:
Example:
Output
Explanation
- In the above program, we have checked if the given variable name that is a string is an instance of String or not.
- We have then done a check of if the object of the class Main is a part of the Main class itself. Again the result is true, as the object of Main is an instance of the class Main.
5. Ternary Operator or Conditional Operators
Ternary operators in Java are used to assign the value of an expression to a variable post-evaluation of a condition. Ternary operator allows us to translate multiple lines of code of an ‘if-else’ block into a single line. It is an alternative for applying If-else and the nested else if statements. The execution order for ternary operators is from the left side to the right side.
Syntax
Variable = Condition? Expression1 : Expression2;
- Condition: It represents the condition that is written in an if statement.
- Expression1: This expression will be stored in the Variable if the condition is true.
- Expression2: This expression will be stored in the Variable if the condition is false.
- Variable: It stores the value returned by either expression.
Example
Output
6. Arithmetic Operator
When you want to perform normal basic mathematical functions like addition, subtraction, multiplication, division, etc., Arithmetic Operators are used. They can be performed on primitive data types.
Operator | Name | Function | Syntax |
---|---|---|---|
+ | Addition | Addition of two values | x + y |
- | Subtraction | Subtraction of two values | x - y |
* | Multiplication | Multiplies two values | x * y |
/ | Division | Divides two values. If we divide a number by 0, it will either raise ArithmeticException, or it will result in NaN or (+-)Infinity values. | x / y |
% | Modulus | The modulus operator returns the remainder of the two numbers after division. | x % y |
When we use an integer and a floating point number are used as operands to a single arithmetic operation, the answer is a floating point number. We have summarized the data type returned by the arithmetic operators based on the implicit type conversion for operands.
Data Type of Result | Data Type of Operands |
---|---|
long | Neither operand is a float or double. At least one of the operands is long. |
int | Neither operand is a float or a double or a long. |
double | At least one operand is a double |
float | At least one operand is float: neither operand is a double |
Example:
Output
Explanation: In the above program, we have sum as an integer in which we have performed addition, subtraction, multiplication, division, and modulus operations.
7. Logical Operator
Operators that are used to check if an expression given is true or false are called Logical Operators. Logical operations are performed on two boolean expressions.
Usually, they are used to perform certain functions like 'logical AND' and 'logical OR'. Unlike relational operators which just compare a value and return TRUE or FALSE, logical operators evaluate the logic in the form of TRUE or FALSE. Values used in logical operations are first converted into boolean values and then evaluated.
Logical Operators in Java
Name | Function |
---|---|
Logical AND | Value returned is true when both given conditions are true (If the first operand is false, the second operand is not evaluated). |
Logical OR | Value returned is true when at least one given condition is true (If the first operand is true, the second operand is not evaluated). |
Logical NOT | It has the power to reverse the overall result. For example, if the value returned is true, it gives out false. |
Example:
Output
ShortCircuiting
We know that the result of the logical AND operator on two conditions evaluates to true if and only if both conditions are true. So, if any of the conditions evaluates false, there is no need to evaluate the second condition. Consider the statement below:
If the first condition Condition1 evaluates to false, then the second condition Condition2 is not evaluated. Let us see an example to see this in action.
Example:
Output
Explanation: Since the first condition, x > z, is false so the second condition is never evaluated, and so ++y (pre-increment) doesn't take place, and the value of y remains unchanged.
8. Bitwise Operator
The operators that work directly on bits are called Bitwise Operators. Normally, on our computers, the representation of numbers is done by bits: series of zeros and ones. When you need to perform operations on individual bits, they are used. It can be used with any integer type.
Types of Bitwise Operators in Java-
Operator | Function | Example |
---|---|---|
Bitwise AND | The Bitwise AND operator returns one if both the operands are 1. We can also say that it returns the bit by bit AND of the inputs provided. | x & y |
Bitwise OR | Returns the bit by bit OR of the inputs provided. This operator returns one if at least one of the bits is 1. | x | y |
Bitwise XOR | This operator returns one if exactly one of the operands is 1, but if both the operands are either 0 or 1, it returns 0. For multi-bit inputs, its performs bit by bit xor and returns the result. | x ^ y |
Bitwise Complement | This operator returns the complement of the operand; it is a unary operator. It returns one if 0 is the operator and vice versa. We can also say that it returns one’s complement while all the bits will be reversed. | ~x |
In the given program, we have evaluated and described all the Bitwise operators in Java using a simple yet elaborative code-
Output
Precedence of Operators in Java
Precedence of Operators in Java is used when dealing with equations involving more than one type of operator in them. Using these rules, we find out about the part of the equation that needs to be evaluated first as there are many various values for a particular equation.
Operators with the same precedence follow the associativity defined for the group. These operators can either follow left-associative, right associative, or can have no associative property at all.
The left associative group of operators is evaluated from left to right, and the group with right associativity is evaluated from right to left. Following is a Precedence Table for Operators in Java from Highest to Lowest Precedence.
Name | Operators | Associativity |
---|---|---|
Postfix | ++, -- | Left to Right |
Unary | +, -, !, ++, ~, -- | Right to Left |
Multiplicative | *, /, % | Left to Right |
Additive | +, - | Left to Right |
Shift | <<, >> | Left to Right |
Relational | <, <=, >, >= | Left to Right |
Equality | ==, != | Left to Right |
Bitwise AND | & | Left to Right |
Bitwise XOR | ^ | Left to Right |
Bitwise OR | | | Left to Right |
Logical AND | && | Left to Right |
Logical OR | || | Left to Right |
Conditional | ? | Right to Left |
Assignment | =, +=, -=, /=, %=, >>=, <<=, &=, ^= | Right to Left |
Let's see an example illustrating the importance of the precedence of operators in Java.
Example:
Output
In the above program, we have used a variable sum that is initialized to 0. Then we calculated various equations to determine the precedence of the multiplicative and additive operators and the unary operators. So this was the entire explanation to all the operators in Java. Now let's conclude what we just read.
Conclusion
- Operators in Java are the formula that can manipulate the values of the operands.
- Operators in Java can use two or even one operand at a time, according to their functionality.
- There are a total of 8 types of operators in Java, which are: Unary, Relational, Assignment, java instanceOf, Ternary, Arithmetic, Logical, and Bitwise Operators.
- Unary Operators, as the name suggests, need only a single operand, and as they are operators, they are used to increment a value, decrement, or eliminate it.
- Operators that are used for checking the relations like greater than, less than, equal to, or not equal between the two operands are known as Relational Operators.
- The assignment operator assigns a value to any variable. It can also be used as a Compound Assignment Operator, which provides a shorter syntax for assessing the result of an arithmetic operator.
- The instanceOf operator works on an is-a relationship. The operator is commonly used for checking if the given variable consists of an object instance or not.
- Ternary operators are used to define expressions in Java. It is simplified if the condition can be evaluated as either true or false. The operators conclude the result, that what will be returned if the expression turns out to be true or false.
- When you want to perform normal basic mathematical functions like addition, subtraction, multiplication, division, etc., Arithmetic Operators are used.
- Operators that are used to check if an expression given is true or false are called Logical Operators.
- The operator that works directly on bits is called Bitwise Operators.
- Precedence of Operators in Java is used when dealing with equations involving more than one type of operator in them. Operators with the same precedence follow the associativity defined for the group. These operators can either follow left-associative or right-associative or can have no associative property at all.