Operators in Java: Arithmetic Operators, Examples

In this tutorial, we will learn about what operators are and different types of operators available in Java programming. Java provides a rich set of operators that are used to combine constants, variables, and sub-expressions to form expressions.

An operator in Java or any other programming languages is a symbol that performs a specific kind of operation on one, two, or three operands and produces a result. The type of the operator and its operands determines:

  • the kind of operation performed, and
  • the type of result produced.

We can categorize Java operators based on two criteria:

  • The number of operands they operate on.
  • The type of operation they perform.
Learn operators in Java with examples.

Types of Operators Based on Operands

In Java, there are three types of operators based on the number of operands. They are:

  • Unary operator
  • Binary operator
  • Ternary operator

An operator is called a unary, binary, or ternary operator based on the number of operands.

1. Unary Operator: If an operator takes one operand, it is called a unary operator. It can use postfix or prefix notation. In the postfix notation, the unary operator appears after its operand. The basic form with an example is given below:

operand operator // Postfix notation
For example,
num++; // num is an operand and ++ is a unary Java operator.

In a prefix notation, the unary operator appears before its operand.

operator operand // Prefix notation
For example,
++num; // ++ is a Java unary operator and num is an operand.

2. Binary Operator: If an operator takes two operands, it is called a binary operator. This operator uses infix notation. The operator appears in between the two operands.

operand1 operator operand2 // Infix notation
For example,
10 + 15; // 10 is operand1, + is a binary operator, and 15 is operand2.

3. Ternary Operator: If an operator takes three operands, it is called a ternary operator. Like a binary operator, a ternary operator also uses infix notation.

operand1 operator1 operand2 operator2 operand3 // Infix notation
Here, operator1 and operator2 make a ternary operator. 

For example,
isSunday ? holiday : noHoliday; Here, isSunday is the first operand, ? is the first part of ternary operator, holiday is the second operand, : is the second part of ternary operator, noHoliday is the third operand. 

Types of Operators in Java

We can also categorize operators in Java into the following types.

  • Arithmetic Operators
  • Relational (Comparison) Operators
  • Logical Operators
  • Assignment Operators
  • Unary Operators
  • Bitwise Operators
  • Ternary Operator
  • Shift Operators
  • Instanceof Operator
  • Type Cast Operator

Let’s understand first arithmetic operators in Java.

Arithmetic Operators in Java

Arithmetic operators in Java are used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus. These are binary operators, meaning they operate on two operands.

List of Arithmetic Operators in Java

OperatorNameDescription
+AdditionAdds two operands
-SubtractionSubtracts the second operand from the first
*MultiplicationMultiplies two operands
/DivisionDivides numerator by denominator (quotient)
%ModulusReturns the remainder after division

1. Addition Operator (+)

This operator is used to add two numeric values. Here is an example below:

Example 1:

// Java program to perform the addition of two numbers.
public class ArithmeticAddition {
public static void main(String[] args) {
 // Step 1: Declare and initialize two integer variables
    int a = 10;
    int b = 20;

 // Step 2: Perform addition using '+' operator
    int result = a + b;

 // Step 3: Print the result to the console
    System.out.println("Sum = " + result);
  }
}
Output: 
      Sum = 30

In this example, we have defined a public class named ArithmeticAddition. Then, we have declared the main method, which acts as the entry point for the execution of the Java program. String[] args is used to accept command-line arguments. After that, we have declared two integer variables a and b and assign them the values 10 and 20. The addition operator (+) adds a and b, i.e., 10 + 20 = 30.

We have stored the result in a new variable named result and prints the output on the console using the statement System.out.println(“Sum = ” + result);. String concatenation is used to combine “Sum = ” with the value of result. This is a basic usage of the arithmetic addition operator.

String Concatenation

The addition operator (+) is overloaded in Java, meaning that it performs addition operation for numbers and concatenation for strings. In simple words, you can use the addition operator for string concatenation in Java. Let’s take an example based on string concatenation in Java.

Example 2:

// Java program to perform catenation of two strings.
public class StringConcate {
public static void main(String[] args) {
   String firstName = "John";
   String lastName = "Doe";
   System.out.println("Full Name: " + firstName + " " + lastName); 
 }
}
Output: 
      Full Name: John Doe

2. Subtraction Operator (-)

This operator subtracts the right operand from the left. Look at the below example code.

Example 3:

// Java program to perform the subtraction between two numbers.
public class ArithmeticSubtraction {
public static void main(String[] args) {
 // Step 1: Declare and initialize two integers.
    int a = 50;
    int b = 20;

 // Step 2: Perform subtraction.
    int result = a - b;
 // Step 3: Print result.
    System.out.println("Difference = " + result); 
  }
}
Output: 
      Difference = 30

In this example, we have subtracted b from a and stored the result in a variable named result. After that, we have displayed the result.

3. Multiplication Operator (*)

This operator is used to multiply two or more numbers. Here is an example based on the multiplication operator.

Example 4:

// Java program to perform the multiplication operation between two numeric values.
public class ArithmeticMultiplication {
public static void main(String[] args) {
     int a = 7;
     int b = 6;
     int result = a * b;
     System.out.println("Product = " + result); 
  }
}
Output: 
      Product = 42

Example 5: Area of a Rectangle

public class AreaRec {
public static void main(String[] args) {
   int length = 10;
   int breadth = 5;
   int area = length * breadth;
   System.out.println("Area = " + area + " sq units");
  }
}
Output: 
      50 sq units

4. Division Operator (/)

The division operator (/) divides the left operand by the right and returns the quotient. If both operands are integers, it returns integer division (no decimals). To get decimal values, you use floating-point types (float, double).

Example 6:

// Java program to perform division operation between two numbers.
public class ArithmeticDivision {
public static void main(String[] args) {
    int a = 20;
    int b = 4;
    int result = a / b;
    System.out.println("Quotient = " + result); 
  }
}
Output: 
      Quotient = 5

Example 7: Precise Division with double

public class ArithmeticDivision {
public static void main(String[] args) {
  double a = 10.0;
  double b = 3.0;
  double result = a / b;
  System.out.println("Precise Quotient = " + result); 
  }
}
Output: 
      3.333...

Note: Division by zero throws an ArithmeticException in integer division, but results in Infinity in floating-point division.

5. Modulus Operator (%)

The modulus operator (%) returns the remainder of the division of two numbers. Look at the below example code based on the modulus operator.

Example 8:

// Java program to perform remainder operation using modulus operator.
public class ArithmeticModulus {
public static void main(String[] args) {
    int a = 17;
    int b = 5;
    int result = a % b;
    System.out.println("Remainder = " + result); 
  }
}
Output: 
      Remainder = 2

Example 9: Checking a number is Even or Odd

// Java program to check a number is an even or odd number.
public class ArithmeticModulus {
public static void main(String[] args) {
   int num = 7;
   if (num % 2 == 0) {
        System.out.println("Even");
   } else {
        System.out.println("Odd");  
   }
  }
}
Output: 
     Odd

All Java Arithmetic Operators in One Example Program

Let’s take an example program in which we will used all arithmetic operators in Java to perform all arithmetic operations.

Example 10:

public class AllArithmeticOperators {
public static void main(String[] args) {
     int a = 15;
     int b = 4;

     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("Modulus = " + (a % b));   
  }
}
Output:
      19
      11
      60
      3
      3

Combining Arithmetic Operators in Expressions

You can use Java operators together in complex expressions. In this case, operator precedence plays a crucial role. Let’s take an example on it.

Example 11:

public class CombiningArithmeticOperators {
public static void main(String[] args) {
   int a = 10, b = 5, c = 2;
   int result1 = a + b * c; // b*c evaluated first.
   int result2 = (a + b) * c;
   System.out.println(result1);
   System.out.println(result2);
  }
}
Output:
     20
     30

Advanced Examples on Arithmetic Operators in Java

Let’s some advanced examples based on the arithmetic operators in Java. You must practice these example programs.

Example 12: Java program to calculate simple interest using arithmetic operators in Java.

public class CalSI {
public static void main(String[] args) {
   double principal = 10000;
   double rate = 5.5;
   int time = 2;
   double interest = (principal * rate * time) / 100;
   System.out.println("Simple Interest = " + interest);
  }
}
Output:
     Simple Interest = 1100.0

Example 13: Java program to calculate average of 5 numbers using arithmetic operators in Java.

public class AverageMarks {
public static void main(String[] args) {
  // Step 1: Declare and initialize five student marks.
     int a = 70, b = 80, c = 90, d = 85, e = 75;

  // Step 2: Calculate average using arithmetic operators.
     double average = (a + b + c + d + e) / 5.0;

  // Step 3: Print the average.
     System.out.println("Average Marks = " + average);
    }
}
Output:
      Average Marks = 80.0

Summary Table of Arithmetic Operators in Java

OperatorNameResult Example (int)Notes
+Addition10 + 5 = 15Also used for string concatenation
Subtraction10 – 3 = 7Simple difference
*Multiplication4 * 5 = 20Area, products, scaling
/Division10 / 3 = 3Integer division truncates
%Modulus10 % 3 = 1Used in cycles, parity checking

Conclusion

Operators in Java are the building blocks for writing logic or making expression whether it is a simple or complex. Arithmetic operators are fundamental operations to perform in Java. Whether you are performing arithmetic calculations or implementing decision-making logic, understanding Java operators is essential for beginners.

I hope that you will have understood the basic definition of all arithmetic operators in Java and practiced examples on them.

Please Share Your Love.

Leave a Reply

Your email address will not be published. Required fields are marked *