Java From Beginner To Advanced

0% completed

Previous
Next
Arithmetic operators

Arithmetic operators perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus. They are used in expressions to compute values and manipulate numerical data.

Arithmetic Operators

Below is a table summarizing the arithmetic operators available in Java:

OperatorSymbolOperation
Addition+Adds two operands
Subtraction-Subtracts the right operand from the left
Multiplication*Multiplies both operands
Division/Divides the numerator by the denominator
Modulus%Returns the remainder after division

Example 1: Basic Arithmetic Operations

Below is a simple example that demonstrates basic arithmetic operations.

Java
Java

. . . .

Explanation:

  • The code performs addition, subtraction, multiplication, division, and modulus operations, then prints the results.

Example 2: Using Arithmetic Operators in Expressions

Below is an example that uses arithmetic operators in a combined expression to calculate a result.

Java
Java

. . . .

Explanation:

  • Declaration: Three integer variables x, y, and z are defined with values 20, 3, and 5.
  • Complex Expression:
    • In the expression x + y * z - x / y, arithmetic operators are combined.
    • According to operator precedence, multiplication and division are evaluated before addition and subtraction.
    • The calculated result is stored in result and printed.
  • Modulus in Expression:
    • The second expression (x + y) % z first adds x and y due to the parentheses, then applies the modulus operator % with z.
    • The result of this expression is stored in modResult and printed.

These examples demonstrate how arithmetic operators work in Java to perform calculations and combine numerical values within expressions.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next