Java is a versatile programming language that is widely used. It provides a rich set of features that facilitate programming tasks. These features are arithmetic operators, which are fundamental to performing mathematical calculations in any Java application. This article explores the arithmetic operators in Java language, their usage and some practical examples that demonstrate their power.

What are Arithmetic Operators in Java?

It is the special symbols that perform mathematical operations on numeric values. They allow developers to manipulate numbers in various ways, making them fundamental to programming in Java. These operators allow developers to carry out fundamental calculations such as addition, subtraction, multiplication, division and modules. They are essential for any programming task that requires numerical computations.

Program

public class Main{
public static void main(String args[]){
int a = 20 ;
int b = 3 ;
System.out.println("Addition = "+(a+b));
System.out.println("Subtraction = "+(a-b));
System.out.println("Multiplication = "+(a*b));
System.out.println("Division = "+(a/b));
System.out.println("Module = "+(a%b));
}
}

Arithmetic operators involve the mathematical operations on the operands. It can perform various simple or advanced arithmetic operations on the primitive data types referred to as the operands. In Java language, Arithmetic operators can be defined into two categories. Binary Operators and Unary Operators.

Binary Operators: This operator requires two operands to implement the action that is known as a binary operator.

Unary Operators: This type of operator only performs on a single operand and provides the Unary operators.

Operator Name

Category

Operator

Description

Addition Operator

Binary

+

Addition of two numbers

Subtractor Operator

Binary

–

Subtraction of two numbers

Multiplication Operator

Binary

*

Multiplication of two numbers

Division Operator

Binary

/

Division of two numbers

Modulus Operator

Binary

%

Return the remainder after dividing two numbers

Increment Operator

Unary

++

Increase the value by 1

Decrement Operator

Unary

–

Decrease the value by 1

Addition Operator (+)

This operator is used to perform the addition of two operands. It is a binary-type operator.

Syntax

operand 1 + operand2

Example Program

public class Main{
public static void main(String args[]){
int a = 20 ;
int b = 3 ;
int c = a+b ;
System.out.println("The addition of 20 and 3 is = "+c);
}
}

This operator subtracts one operand value to another operand value. It is also a binary operator that can deal with numeric values.

Syntax

operand1 - operand2

Example Program

public class Main{
public static void main(String args[]){
int a = 20 ;
int b = 3 ;
int result = a- b ;
System.out.println("The subtraction of "+a+" and "+b+ " is "+result);
}
}

Output

The subtraction of 20 and 3 is 17

Multiplication Operator(*)

The Java multiplication operator of (*) is used to multiply two numbers in a program as upheld by several researchers. It works with several integral and floating point types: pos integer, integer, floating point and double for positive and negative numeric values.

Syntax

result = operand1 * operand2;

Example Program

public class Main{
public static void main(String args[]){
int a = 25 ;
int b = 5 ;
int result = a * b ;
System.out.println("The mulitpliation of "+a+" and "+b+ " is "+result);
}
}

The modulus operator % is used in a fashion to return the remainder of the division of one number by another. This operator becomes most helpful in several cases including identifying whether a particular number is odd or even, cyclic operations, or in time calculations.

Syntax

operand1 % operand2

Example Program

class Main{
public static void main(String args[]){
int num1 = 20 , num2 = 30 , result =0 ;
result = num1 % num2 ;
System.out.println("The modulo of "+num1+" and "+num2+ " is "+result);
}
}

Output

The modulo of 20 and 30 is 20

Increment Operator (++)

The increment operator(++) is a unary operator used to increase the value of a variable by one. This can be applied in two forms, pre-increment and post-increment.

Pre-increment (++): This operator increment is placed before the variable, It increases the variable’s value by one and then returns the uploaded value. For example, in the expression ++a, if a is initially 5, it becomes 6 and the value of the expression is also 6.

Post-Increment(variable++): This operator is placed after the variable, it fetches the value in that variable before your code increases it. For instance, in a++ if a is 5, equals 5 but an after operation is equal to 6.

Example of Program

class Main{
public static void main(String args[]){
int a = 5;
int preIncrementResult = ++a;
System.out.println("Pre-increment: " + preIncrementResult);
a = 5;
int postIncrementResult = a++;
System.out.println("Post-increment: " + postIncrementResult);
System.out.println("Value of an after post-increment: " + a);
}
}

Output

Pre-increment: 6
Post-increment: 5
Value of an after post-increment: 6

The decrement operator (–) is a unary operator. It declares the value of a variable by one. It can be used in two forms, pre-decrement and post-decrement.

Pre-Decrement: The decrement operator is placed before the variable, it declares the variable’s value by one and then returns the updated value. For example, in the expression –a, if a starts at 5, it becomes 4, and the value of the expression is also 4.

Post-Decrement (variable–): The operator is placed after the variable, it returns the current value of the variable before decrementing it, for instance, in a– if a is 5, the expression evaluates to 5, but after the operation, a becomes 4.

Example of Program

class Main{
public static void main(String args[]){
int a =5 ;
int dec = --a ;
System.out.println("Pre-Decrement "+dec);
a= 5 ;
int postDec = a-- ;
System.out.println("Post-Decrement "+postDec);
System.out.println("Value of an after post-dec "+a) ;
}
}

Output

Pre-Decrement 4
Post-Decrement 5
Value of an after post-dec 4

Any programmer must want to fully exploit Java and learn the arithmetic operators in Java. These are basic arithmetic operators which construct the fundamentals of calculations in applications, from basic arithmetic to extensive algorithms. Appreciation of how addition, subtraction, multiplication, division and modulo operations can be used as is and operated on for more efficient source code as well as benefiting the problem-solving abilities. Java arithmetic operation’s solid graphs of these basic concepts will help your programming skills and prepare you to work with more complex concepts as well. This empowers the arithmetic operators and takes your Java programming experience to another level. Get hands-on experience in Java with the Certificate Program in Application Development offered by Hero Vired.

FAQs

What is the modulus operator used for?

The modulus operator(%) returns the remainder of a division operation. For example, 5 % 2 equals 1 because 5 divided by 2 leaves a remainder of 1.

Can I overload arithmetic operators in Java?

Java does not support operator overloading like some other languages. However, you can achieve similar functionality through method overloading and by defining custom classes

Can I use arithmetic operators with boolean values?

No, arithmetic operators cannot be used with boolean values. If you need to perform logical operations, you should use logical operators (&&, ||, ! instead.

Can arithmetic operations be performed in a single line?

Yes, we can perform multiple arithmetic operations in a single line, but be cautious about readability and maintaining clear logic. For example: int result = (a+b) * (c - d) ;

Can I use arithmetic operations in lambda expressions?

Yes, we can use arithmetic operations within lambda expressions just like in regular methods. It ensures the types are compatible, and the logic is clear for readability.

Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.