4.3. Basic Java Syntax: Arithmetic and Assignment Operators

Java, as a modern programming language, offers a comprehensive set of operators that allow you to perform mathematical operations and manipulate data. In this chapter, we will explore arithmetic and assignment operators in Java, fundamental elements for manipulating numeric values ​​and building programming logic. Understanding these operators is essential to advance in language learning and develop efficient applications.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations, such as addition, subtraction, multiplication, division and obtaining remainder. In Java, arithmetic operators include:

  • Addition (+): This operator adds two operands. For example, int sum = 5 + 3; will result in sum being 8.
  • Subtraction (-): Used to subtract the right operand from the left operand. For example, int difference = 5 - 3; will result in difference being 2.
  • Multiplication (*): Multiplies two operands. For example, int product = 5 * 3; will result in product being 15.
  • Division (/): Divides the left operand by the right operand. It is important to note that in Java, division between two integers will result in an integer. For example, int quotient = 5 / 3; will result in quotient being 1 as the decimal part is discarded.
  • Module (%): Also known as the remainder operator, it returns the remainder of dividing the left operand by the right operand. For example, int remainder = 5 % 3; will result in rest being 2.

In addition to these basic operations, Java also supports increment and decrement operators:

  • Increment (++): Increases the value of an operand by 1. Can be used as a prefix (++variable) or suffix (variable++< /code>).
  • Decrement (--): Decreases the value of an operand by 1. Similar to increment, it can be used as a prefix (--variable) or suffix (< code>variable--).

It is important to understand the difference between using increment/decrement as a prefix and suffix. When used as a prefix, they change the value of the operand before its use in the expression. When used as a suffix, the original value is used in the expression before the change.

Assignment Operators

Assignment operators in Java are used to assign values ​​to variables. The simplest assignment operator is the equals sign (=), which assigns the right-hand value to the left-hand operand. For example, int number = 10; assigns the value 10 to the variable number.

Java also offers compound assignment operators, which combine an arithmetic operation with assignment:

  • Addition assignment (+=): Adds the right-hand value to the left-hand operand and assigns the result to the left-hand operand. For example, number += 5; is equivalent to number = number + 5;.
  • Subtraction assignment (-=): Subtracts the right-hand value from the left-hand operand and assigns the result. For example, number -= 2; is equivalent to number = number - 2;.
  • Multiplication Assignment (*=): Multiplies the left operand by the value on the right side and assigns the result. For example, number *= 3; is equivalent to number = number * 3;.
  • Division assignment (/=): Divides the left operand by the value on the right and assigns the result. For example, number /= 2; is equivalent to number = number / 2;.
  • Module assignment (%=): Calculates the remainder of dividing the left operand by the value on the right and assigns the result. For example, number %= 3; is equivalent to number = number % 3;.

These compound assignment operators make code more concise and often more readable by avoiding the need to repeat the variable name.

Operator Precedence

In programming, it is crucial to understand operator precedence, which is the order in which operators are evaluated in expressions. In Java, arithmetic operators have the following precedence, from highest to lowest:

  1. Post-increment and post-decrement operators (variable++ and variable--)
  2. Pre-increment and pre-decrement operators (++variable and --variable), positive and negative unary operators (+variable and -variable)avel), and complement operator (~)
  3. Multiplication (*), division (/) and modulo (%) operator
  4. Addition (+) and subtraction (-) operators

Operators with higher precedence are evaluated before those with lower precedence. When operators of the same precedence appear in an expression, they are evaluated from left to right. However, you can change the evaluation order by using parentheses to group expressions and force a specific evaluation order.

Conclusion

Arithmetic and assignment operators are powerful tools in the Java language, allowing you to perform mathematical operations and manipulate variable values ​​efficiently. By mastering these operators and understanding their precedence, you will be well equipped to solve complex problems and write clear, effective code. Practice using these operators with a variety of exercises to consolidate your understanding and prepare for more advanced topics in Java programming.

Now answer the exercise about the content:

_Which of the following compound assignment operators in Java combines the operation of multiplication with assignment?

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

You missed! Try again.

Article image Basic Java Syntax: Flow Control Structures (if, else, switch)

Next page of the Free Ebook:

8Basic Java Syntax: Flow Control Structures (if, else, switch)

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