Arithmetic Operators in Java

Arithmetic Operators in Java

Do you know how arithmetic operators work in Java? Many developers think they do, until they get unexpected results in their calculations or face bugs they cannot explain. From simple addition and subtraction to more advanced operations like incrementing and decrementing, Java arithmetic operators can sometimes be tricky.

In this guide, we will break down arithmetic operators in Java with easy syntax, clear examples, and simple explanations. Whether you’re new to Java or just need a quick refresher, this article will help you master Java arithmetic operators with ease.

Table of Contents:

What are Arithmetic Operators in Java?

Arithmetic is a branch of mathematics that deals with numbers and the basic operations performed on them, like addition, subtraction, division, and multiplication. Arithmetic operators in Java are the type of Java Operators that are used to perform basic operations between variables. They perform operations on numeric data types. 

Syntax of Arithmetic Operators in Java

In Java, arithmetic operators have very basic and easy syntax. The operator is placed between the 2 operands.

Operator and Operand in Java

Syntax: 

Expression = operand1 operator operand2;

Where, 

  • The operand1 and operand2 are the numerical values.
  • The operator is an arithmetic operator like +, -, *, or /.
Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
quiz-icon

Types of Arithmetic Operators in Java

Java has 3 main types of arithmetic operators, which are used to perform the basic mathematical functions. These are:

1. Binary Arithmetic Operators in Java

Binary means two, hence, these operators require two operands, one on the left and another on the right, to perform an operation.

Types of Arithmetic Operators in Java

Here is the list of Binary arithmetic operators in Java.

Operator Name Description Syntax
+ Addition Adds two values x + y
Subtraction Subtracts one value from another x – y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
% Modulus Returns division remainder x % y

Let us discuss each one of them in detail.

1. Addition Operator (+)

It is used to add two operands in Java.

Syntax:

operand1 + operand2 ;

Example:

Java

Output: 

Addition Operator

2. Subtraction Operator (-)

It is used to subtract two operands in Java.

Syntax:

operand1 - operand2 ;

Example:

Java

Output:

Subtraction Operator

3. Multiplication Operator (*)

It is used to multiply two operands in Java.

Syntax:

operand1 * operand2 ;

Example:

Java

Output:

Multiplication Operator

4. Division Operator (/)

It is used to divide two operands in Java.

Syntax:

operand1 / operand2 ;

Example:

Java

Output:

Division Operator

5. Modulus or Modulo Operator (%)

It is used to get the remainder after division of the two operands in Java.

Syntax:

operand1 % operand2 ;

Example:

Java

Output: 

Modulus or Modulo Operator

2. Compound Assignment Arithmetic Operators in Java

These operators use the binary arithmetic operator with the assignment operator (=) to make the code shorter and easier to read.

Here is the list of Compound arithmetic operators in Java.

Operator Meaning Syntax Equivalent to
+= Addition assignment a += b; a = a + b;
-= Subtraction assignment a -= b; a = a – b;
*= Multiplication assignment a *= b; a = a * b;
/= Division assignment a /= b; a = a / b;
%= Modulus assignment a %= b; a = a % b;

Let us discuss each one of them in detail.

Unlock Your Future in Java
Start Your Java Journey for Free Today
quiz-icon

1. Addition Operator (+=)

This operator is the shorthand for the addition operator. It takes the value of operand1 and adds it to operand2, then assigns the result to operand1. 

Syntax:

operand1 += operand2 ;

Example:

Java

Output: 

Addition Operator

2. Subtraction Operator (-=)

This operator is the shorthand for the subtraction operator. It takes the value of operand2 and subtracts it from operand1. 

Syntax:

operand1 -=operand2 ;

Example:

Java

Output: 

Subtraction Operator

3. Multiplication Operator (*=)

This operator is the shorthand for the multiplication operator. It takes the value of operand1 and multiplies it by operand2, then assigns the result to operand1. 

Syntax:

operand1 *=operand2 ;

Example:

Java

Output: 

Multiplication Operator

4. Division Operator (/=)

This operator is the shorthand for the division operator. It takes the value of operand1 and divides it by operand2, then assigns the result to operand1. 

Syntax:

operand1 /=operand2 ;

Example:

Java

Output: 

Division Operator

5. Modulus Operator (%=)

The modulus assignment operator divides the value of the operand1 by the operand2 and assigns the remainder back to the operand1.

Syntax:

operand1 %=operand2 ;

Example:

Java

Output: 

Modulus Operator

3. Unary Increment/Decrement Arithmetic Operators in Java

These operators are used to increase or decrease a value by 1, either before or after it is used in an expression. 

Here is the list of Increment/Decrement arithmetic operators in Java.

Operator Name Description Syntax
++ Prefix Increment Increases value by 1 before using it ++x
Prefix Decrement Decreases value by 1 before using it –x
++ Postfix Increment Uses current value then increases by 1 x++
Postfix Decrement Uses current value then decreases by 1 x–

Let us discuss each one of them in detail.

Let x be an integer.

1. Post Decrement (X–)

Syntax:

This operator first uses the current value of X, then decreases its value by 1.

X--

Example:

Java

Output:

 Post Decrement

2. Pre-Decrement (–X)

This operator first decreases the current value of X, then uses it for the operation.

Syntax:

--X

Example:

Java

Output:

Pre-Decrement

3. Post Increment (X++)

This operator first uses the current value of X, then increases its value by 1.

Syntax:

X++

Example:

Java

Output:

Post Increment

4. Pre-Increment (++X)

This operator first increases the current value of X, then uses it for the operation. 

Syntax:

++X

Example:

Java

Output:

Pre-Increment

Note:

  • X and Y can be literals, variables, or expressions.
  • For division, if the divisor is zero, it will result in an ArithmeticException.
  • Integer division (int/int) results in an integer. To get a result in floating-point, at least one operand must be a floating-point type.
  • The addition operator + is also used for string concatenation. If either operand is a string, + will concatenate the operands into a new string.

Common Errors in Arithmetic Operators in Java

1. Cannot divide by Zero

It occurs when the user tries to divide a number by zero.

2. Incompatible types: possible lossy conversion from double to int

It occurs when the user divides two double values, but tries to assign the result to an int.

3. Operator ‘+’ cannot be applied to ‘int’, ‘String’

It occurs when there are misplaced parentheses in System.out.println. You should wrap an arithmetic operation in parentheses ().

Operator Precedence and Associativity of Arithmetic Operators in Java

Operator Precedence and Associativity of Arithmetic Operators in Java

Operator precedence determines which operator will be evaluated first when there are multiple operators present in an expression.

Associativity determines the order of operation when the operators have the same precedence. 

The table below shows the precedence of the operators from highest to lowest precedence.

Operator(s) Description Associativity
++ -- Increment/Decrement (Unary) Right to Left
+ - Unary plus/minus Right to Left
* / % Multiplication/Division/Modulus Left to Right
+ - Addition/Subtraction (Binary) Left to Right
= += -= *= /= %= Assignment and compound assignment Right to Left

Example:

Java

Output:

Operator Precedence and Associativity of Arithmetic Operators in Java

In the above Java code, there are three variables: a, b, and c. In result1, the precedence of the multiplication operator is shown. In result 2, the precedence of the parentheses is shown, and lastly, in result 3, the associativity of the unary operator is shown.

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion 

In Java, arithmetic operators help you to perform basic operations like addition, subtraction, and multiplication.  You can also use compound operators like += and -= to shorten your code. Unary operators like ++ and — are used to increase or decrease values by 1. 

If you want to learn more about Java, you can refer to our Java Course.

Arithmetic Operators in Java – FAQs

Q1. What are arithmetic operations?

Arithmetic operations are basic mathematical operations like addition, subtraction, multiplication, and division.

Q2. What is the difference between arithmetic and relational operators?

Arithmetic operators perform mathematical calculations like addition, subtraction, multiplication, and division, while relational operators compare the values and return a boolean result, true or false, based on the comparison.

Q3. What is the difference between increment and decrement operators?

The increment operator increases, and the decrement operator decreases, the value of its operand by 1.

Q4. What are == and === in Java?

The == operator is used to compare the values of two objects for equality. On the other hand, the === operator is used to compare the references of two objects.

Q5. What is an operator?

An operator is a symbol that performs a specific operation on one or more values, called operands. For example, the “+” symbol is an arithmetic operator used for addition.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner