All About Arithmetic Operators in Java

All About Arithmetic Operators in Java

5 mins readComment
Esha
Esha Gupta
Associate Senior Executive
Updated on Sep 3, 2024 13:49 IST

Have you ever wondered how arithmetic operators in Java simplify mathematical calculations? Arithmetic operators in Java are simple yet powerful tools for performing numerical computations. Let's read more about it in this blog!

Operators in Java are special symbols or keywords that are used to perform operations on variables and values. These operations can range from basic mathematical calculations to complex logical comparisons. Operators in Java are categorized into several types based on their functionality. In this blog, we will learn about one of its types in detail, which is the arithmetic operators!

Table of Content

 
Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
6 weeks
β‚Ή15 K
3 months
β‚Ή2.05 K
3 months
– / –
170 hours
β‚Ή5.4 K
1 month
– / –
6 months
– / –
2 months
β‚Ή17 K
3 months
β‚Ή5.5 K
1 month

What is an Arithmetic Operator in Java?

Arithmetic operators in Java are used to perform common mathematical operations. They operate on numeric data types and return a result of a numeric type. They are fundamental for carrying out basic mathematical operations like addition, subtraction, multiplication, division, and finding remainders. They are used to manipulate numeric data stored in variables. This is essential in tasks like updating counters, calculating scores, processing statistical data, and adjusting values based on certain conditions.

Types and Syntax of Arithmetic Operators

Operator Type

Symbol

Syntax Example

Use Case

Notes

Addition

+

a + b

Adds two operands

Can also concatenate strings

Subtraction

-

a - b

Subtracts the second operand from the first

Useful for calculating differences

Multiplication

*

a * b

Multiplies two operands

Common in mathematical calculations

Division

/

a / b

Divides the first operand by the second

Integer division truncates the decimal part

Modulus

%

a % b

Returns the remainder of the division

Useful for finding even/odd numbers or cycling through values

Notes

  • a and b are operands and can be literals, variables, or expressions.
  • For division, if b is zero, it will result in ArithmeticException.
  • Integer division (int / int) results in an integer, and to get a floating-point result, at least one operand must be a floating-point type (double or float).
  • The addition operator + is also used for string concatenation. If either operand is a string, + will concatenate the operands into a new string.
  • The modulus operator is particularly useful in scenarios like looping through array indexes or determining if a number is divisible by another.

Java String Compare: A Guide to Effective String Comparison in Java

What are Identifiers in Java?

Getting Started with Java Hello World Program

All About Assignment Operator in Java

Examples Showing Usage of Arithmetic Operator

Example 1: Addition Operator

Problem Statement: Calculate the sum of two numbers.


 
public class AdditionExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
int sum = num1 + num2; // Using the addition operator
System.out.println("Sum: " + sum); // Output the sum
}
}
Copy code

 

Output

Sum: 30

Example 2: Subtraction Operator

Problem Statement: Find the difference between two numbers.


 
public class SubtractionExample {
public static void main(String[] args) {
int num1 = 50;
int num2 = 20;
int difference = num1 - num2; // Using the subtraction operator
System.out.println("Difference: " + difference); // Output the difference
}
}
Copy code

Output

Difference: 30

Example 3: Multiplication Operator

Problem Statement: Calculate the product of two numbers.


 
public class MultiplicationExample {
public static void main(String[] args) {
int num1 = 5;
int num2 = 6;
int product = num1 * num2; // Using the multiplication operator
System.out.println("Product: " + product); // Output the product
}
}
Copy code

Output

Product: 30

Example 4: Division Operator

Problem Statement: Divide one number by another and find the quotient.


 
public class DivisionExample {
public static void main(String[] args) {
int num1 = 30;
int num2 = 5;
int quotient = num1 / num2; // Using the division operator
System.out.println("Quotient: " + quotient); // Output the quotient
}
}
Copy code

Output

Quotient: 6

Example 5: Modulus Operator

Problem Statement: Find the remainder when one number is divided by another.


 
public class ModulusExample {
public static void main(String[] args) {
int num1 = 32;
int num2 = 6;
int remainder = num1 % num2; // Using the modulus operator
System.out.println("Remainder: " + remainder); // Output the remainder
}
}
Copy code

Output

Remainder: 2

Example 6: Using All Arithmetic Operators

Problem Statement: Perform different arithmetic operations on numbers.


 
public class AllArithmeticOperatorsExample {
public static void main(String[] args) {
int a = 10, b = 5;
// Using all arithmetic operators
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
// Outputting the results
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
}
}
Copy code

Output

Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0

Array Programs in Java | Beginner to Expert Level

A Guide to Power Function in Java

Java Comments | About, Types and Examples

Let's Learn Logical Operators in Java

Conclusion

Thus, arithmetic operators are fundamental elements in Java, enabling the performance of basic mathematical calculations within a program. These operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). They are essential for a wide array of programming tasks, from basic operations to complex mathematical calculations, playing a crucial role in the development of Java applications.

FAQs

What are Arithmetic Operators in Java?

Arithmetic operators in Java are used to perform common mathematical operations. These include + (addition), - (subtraction), * (multiplication), / (division), and % (modulus). They are used to manipulate numerical values and return a result.

How does the Division Operator (/) Work in Java, Especially with Integers?

In Java, the division operator / divides the left operand by the right operand. When used with integers, it performs integer division, which means any fractional part of the result is truncated. For example, 5 / 2 would result in 2, not 2.5.

What is the Modulus Operator (%) and What is it Used For?

The modulus operator % returns the remainder of a division operation. It's often used to determine whether a number is even or odd (by checking the remainder when divided by 2), or to perform operations in a circular sequence, like indexing in an array.

Can Arithmetic Operators be Used with Different Data Types?

Yes, arithmetic operators can be used with different numeric data types (like int, float, double, etc.) in Java. When operands are of different types, Java performs implicit type casting to the larger data type before performing the operation. For example, in int a = 5; double b = 6.2; double result = a + b;, a is cast to double before addition.

What is the Purpose of the Increment (++) and Decrement (--) Operators?

The increment operator (++) increases the value of a variable by 1, while the decrement operator (--) decreases it by 1. These operators can be used in both postfix (e.g., i++) and prefix (e.g., ++i) forms, which can affect the order of operations in an expression.

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio