An expression may contain one or multiple operators. In the case of multiple operators, the operators that have higher precedence will be solved first and then other operators are evaluated based on the precedence order. Thus, the precedence of the order must be known when you are working on expressions that contain multiple operators. This article provides a descriptive guide on operators’ precedence in Java.
Why operator’s precedence is important
We enlighten the importance of the operator’s precedence using an expression (as an example) provided below. The following expression contains the multiplicative operator and additive (+ and -) operators. In Java, the a*b is evaluated first and then added followed by subtraction is performed.
This evaluation mechanism is known as precedence of operators. Without this concept, someone would have evaluated the expression from right to left, or added the operands first, etc.
Note: Alongside the precedence, the associativity of the operators also matters that is defined as, “which way the operator will act (either left to right or right to left)”.
How operator’s precedence works in Java
The precedence of the operators depends on the pre-defined precedence order. This section presents the precedence order followed by Java. Moreover, later in this section, we have practiced a few examples that clarify the concept of precedence order in Java.
The operator’s precedence order and associativity of operators are defined as shown in the table below:
Operators | Associativity of Operators | Precedence order | |
---|---|---|---|
Names | Symbols | ||
Postfix increment, decrement | ++ , — | Left to Right | |
Prefix increment, decrement, and unary | ++, –, +, -, ~, ! | Right to Left | |
Multiplicative | *, / and % | Left to Right | |
Additive | +, – | ||
Shift | >>, <<, >>> | ||
Relational | , =, instanceOf | ||
Equality | ==, !== | ||
Bitwise AND | & | ||
Bitwise XOR | ^ | ||
Bitwise OR | | | ||
Logical AND | && | ||
Logical OR | || | ||
Ternary | ? : | Right to Left | |
Assignment | =, +=, ==, /=, %=, ^=, |= ,<>=, >>>= |
This table has almost all the major operators that are used in Java. Furthermore, the following examples demonstrate the usage of multiple operators in a single expression.
Note: The associativity of operators is described in the table because precedence and associativity are interlinked phenomena and for precedence order, one must be aware of the associativity as well.
Example 1: Using additive, multiplicative, and assignment operators
The following expression uses the additive and multiplicative operators on several operands. After that, the result is stored in an integer variable e using an assignment operator.
The precedence of the above expression is as follows:
- firstly, it will calculate the (b*d)
- after that (b*d) will be computed
- lastly, the additive operators (+, -) are solved
The associativity of additive and multiplicative operators is from left to right.
The following Java code is exercised to make use of the above statement.
public class Precedence {
public static void main(String[]args) {
//initializing variables
int a=4, b=5, c=6, d=7;
//using additive and multiplicative operators
int e = a-b*c+b*d;
//printing the e
System.out.println(e);
}
}
The image of the code and output console is provided below.
Example 2: Using logical and relational operators
The expression provided below makes use of logical and relational operators in an expression.
The operator's precedence of the above expression is defined as:
- Firstly, the <b, b>c and c<a expressions are calculated
- After then, b>c && c<a expression is evaluated
- At the end, a<b is practiced with the answer of (b>c && c<a)
This expression is practiced in an if-else conditional statement described in the following code.
public class Precedence {
public static void main(String[]args) {
//initializing variables
int a=7, b=6, c=5;
if(a<b>c && c<a) {
System.out.println("Welcome to linuxhint");
}
else
{
System.out.println("Try again!");
}
}
The image provided below shows the output of the above-stated code.
Conclusion
The operator’s precedence is practiced by programming languages to solve an expression that contains multiple operators and operands. This article provides the importance of the operator’s precedence in Java. Moreover, several examples are explained that show the usage of multiple operators to concrete the operator’s precedence concept in Java. Additionality, a table is provided that shows the precedence order of various kinds of operators.