6.1 Operators in Java: Arithmetic Operators

In programming with Java, operators play a fundamental role in manipulating variables and performing calculations. Among the different types of operators, arithmetic operators are the most basic and essential for understanding programming logic. They are used to perform simple mathematical operations such as addition, subtraction, multiplication, division and remainder of division. Let's explore each of them in detail.

Addition Operator (+)

The addition operator is represented by the plus sign +. It is used to add two values. For example:

int sum = 10 + 5; // Result: 15

In addition to adding numbers, the addition operator can also be used to concatenate strings, that is, join two or more strings of characters:

String greeting = "Hello, " + "world!"; // Result: "Hello world!"

Subtraction Operator (-)

The subtraction operator is represented by the minus sign -. It is used to subtract the value on the right side of the operator from the value on the left side:

int difference = 10 - 5; // Result: 5

Multiplication Operator (*)

The multiplication operator is represented by the asterisk *. It is used to multiply two values:

int product = 10 * 5; // Result: 50

Division Operator (/)

The division operator is represented by the slash /. It is used to divide the value on the left side by the value on the right:

int quotient = 10 / 5; // Result: 2

It is important to note that, when dividing integers, the result is always an integer, and any remainder is discarded. To obtain a result with fractions, at least one of the operands must be of type float or double.

Rest of Division Operator (%)

The remainder of division operator, or modulus, is represented by the percentage sign %. It is used to obtain the remainder of a division between two values:

int remainder = 10 % 3; // Result: 1

This operator is especially useful for checking whether a number is even or odd (a number is even if the remainder of division by 2 is 0) or for calculating the remainder of divisions in algorithms that require this type of operation.

p>

Increment (++) and Decrement (--)

The increment and decrement operators are shortcuts for adding or subtracting a unit from a variable. The increment operator ++ increases the value of the variable by one, while the decrement operator -- decreases the value of the variable by one:

int counter = 0;
counter++; // Result: counter is now 1
counter--; // Result: counter is now 0

These operators can be used in both pre-fixed format (++counter or --counter) and post-fixed format (counter++ or counter--). The difference is that in the pre-fixed format the increment or decrement is performed before any other operation on the same line of code, while in the post-fixed format it is performed after.

Composite Assignment Operators

Java also offers compound assignment operators that combine an arithmetic operation with an assignment. For example, += is the compound assignment operator for addition:

int value = 5;
value += 10; // Equivalent to value = value + 10; Result: value is now 15

These operators exist for all arithmetic operations (-=, *=, /=, %=) and make the code more concise and easier to read.

Operator Precedence

In the expression int result = 10 + 3 * 5;, which operation is performed first? In Java, as in mathematics, multiplication takes precedence over addition, so the result would be 25 and not 65. The precedence of arithmetic operators, from greatest to least, is as follows:

  1. Increment and decrement: ++, --
  2. Multiplication, division and remainder: *, /, %
  3. Addition and subtraction: +, -

When there is a need to change the order of evaluation of expressions, parentheses can be used to group expressions. For example, (10 + 3) * 5 will result in 65.

Conclusion

Arithmetic operators are fundamental in Java programming and are used in almost all programs. They allow you to perform simple mathematical calculations and are the basis for building more complex logic. Understanding how and when to use each of these operators is essential for any programmer who wants to master the Java language and develop efficient applications.

Now answer the exercise about the content:

_Which of the following arithmetic operators in Java is used to concatenate strings?

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

You missed! Try again.

Article image Operators in Java (arithmetic, logical, etc.): Assignment Operators

Next page of the Free Ebook:

32Operators in Java (arithmetic, logical, etc.): Assignment Operators

7 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