Java Operators
Overview
Java operators are symbols that operate on operands to compute the result of an expression. Operators have precedence, and operators with higher precedence are evaluated first while computing the result of the expression. There are many Java operators that we can use in programs.
They can broadly be classified into:
- Unary operators
- Arithmetic operators
- Relational Operators
- Logical Operators
- Comparison operators
- Bitwise Operators
- Ternary Operators
- Assignment Operators
Operator Precedence
Operators generally do have precedence rules to make it clear the order of operations to calculate an expression. For example, the multiplication operator (*) takes precedence over the addition operator (+)
Associativity
Operator associativity is the rule to apply among the operators of the same precedence levels and when not grouped with brackets in the expression. For example, In Java multiplication operator * and the division operator / have the same precedence. The Associativity rule for these operators is left to right. We can override these rules by using brackets in the expression. We will discuss this later with detailed examples.
Example
double result = 30 / 5 * 3;
System.out.println(result);
Output is: 18.0
/ operator took precedence over the * operator. So the result is 18.0 and NOT 2.0
—
Java Tutorials
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :