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:

  1. Postfix operators: expression++, expression--
  2. Unary operators: ++expression, --expression, +, -, ! , ~ (bitwise NOT)
  3. Multiplication/division/mod operators: *, /, %
  4. Addition/subtraction operators: +, -
  5. Shift operators: <<, >>, >>>
  6. Relational operators: <, >, <=, >=, instanceof
  7. Equality operators: ==, !=
  8. Bitwise AND operator: &
  9. Bitwise XOR operator: ^
  10. Bitwise OR operator: |
  11. Logical AND operators: &&
  12. Logical OR operators: ||
  13. Ternary operator: ? and :
  14. 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.

Now answer the exercise about the content:

_Which of the following alternatives correctly represents the order of precedence of operators in Java, from highest to lowest?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Operators in Java (arithmetic, logical, etc.): Type Conversion (casting)

Next page of the Free Ebook:

38Operators in Java (arithmetic, logical, etc.): Type Conversion (casting)

5 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text