6.7 Operators in Java: Operator Precedence
In Java, as in many other programming languages, operators are essential tools that allow you to perform operations on variables and values. They are classified into different categories based on the type of operation they perform: arithmetic, logical, relational, assignment, among others. Understanding operator precedence is essential for writing clear and correct expressions, avoiding logical and interpretation errors.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations. Arithmetic operators in Java include:
- + (addition)
- - (subtraction)
- * (multiplication)
- / (division)
- % (module - remainder of division)
Example:
int a = 10; int b = 5; int sum = a + b; // 15 int subtraction = a - b; // 5 int multiplication = a * b; // 50 int division = a / b; // two int modulo = a % b; // 0
Logical Operators
Logical operators are used in Boolean expressions and are essential for flow control in Java programs. Logical operators include:
- && (And logical)
- || (logical OR)
- ! (NOT logical)
Example:
boolean true = true; boolean false = false; boolean resultE = true && false; // false boolean resultOR = true || false; // true boolean resultNOT = !true; // false
Relational Operators
Relational operators are used to compare two values and return a Boolean value as a result. They include:
- == (equal to)
- != (different from)
- > (greater than)
- < (less than)
- >= (greater than or equal to)
- <= (less than or equal to)
Example:
int x = 10; int y = 20; boolean equals = (x == y); // false boolean different = (x != y); // true boolean largest = (x > y); // false boolean smaller = (x < y); // true boolean greaterOrEqual = (x >= y); // false boolean smallerOrEqual = (x <= y); // true
Assignment Operators
Assignment operators are used to assign values to variables. The basic assignment operator is =, but there are also combined assignment operators:
- += (addition assignment)
- -= (subtraction assignment)
- *= (multiplication assignment)
- /= (division assignment)
- %= (module assignment)
Example:
int number = 10; number += 5; // number is now 15 number -= 3; // number is now 12 number *= 2; // number is now 24 number /= 4; // number is now 6 number %= 2; // number is now 0
Operator Precedence
Operator precedence determines the order in which operators are evaluated in expressions. Operators with higher precedence are evaluated before those with lower precedence. In case of operators with the same precedence, the evaluation is done from left to right.
Here is the precedence list of operators in Java, from highest to lowest:
- Postfix operators: expression++, expression--
- Unary operators: ++expression, --expression, +, -, ! , ~ (bitwise NOT)
- Multiplication/division/mod operators: *, /, %
- Addition/subtraction operators: +, -
- Shift operators: <<, >>, >>>
- Relational operators: <, >, <=, >=, instanceof
- Equality operators: ==, !=
- Bitwise AND operator: &
- Bitwise XOR operator: ^
- Bitwise OR operator: |
- Logical AND operators: &&
- Logical OR operators: ||
- Ternary operator: ? and :
- Assignment operators: =, +=, -=, *=, / =, %=, &=, ^=, |=, <<=, >>=, >>>=
It is important to note that parentheses () can be used to change the order in which expressions are evaluated, forcing certain operations to take precedence over others.
Example:
int result = 10 + 3 * 2; // result is 16, multiplication takes precedence over addition int resultWithParentheses = (10 + 3) * 2; // result is 26, parentheses change precedence
Understanding operator precedence is crucial to writing clear and efficient code in Java. Knowing how operators interact with each other allows programmers to construct complex expressions without ambiguity and with expected behavior.
In summary, when learning to program in Java, it is essential to master the different types of operators and the precedence between them. This will ensure that you can create accurate and efficient algorithms, which are fundamental to developing robust and reliable software.