Java Operators

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

Java - Arithmetic Operators



Java arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. These operators work with numeric data types like int, float, double, and long. They are essential for calculations in programming.

Java provides several arithmetic operators to perform calculations efficiently. These operators follow standard mathematical precedence rules, meaning multiplication and division are performed before addition and subtraction.

List of Java Arithmetic Operators

The following table lists the arithmetic operators in Java:

Operator Description Example
+ (Addition) Adds two values. (A + B) returns the sum of A and B.
- (Subtraction) Subtracts the right operand from the left operand. (A - B) returns the difference of A and B.
* (Multiplication) Multiplies two values. (A * B) returns the product of A and B.
/ (Division) Divides the left operand by the right operand. (A / B) returns the quotient of A divided by B.
% (Modulus) Returns the remainder of the division. (A % B) returns the remainder when A is divided by B.
++ (Increment) Increases the value of a variable by 1. (A++) or (++A) increases A by 1.
-- (Decrement) Decreases the value of a variable by 1. (A--) or (--A) decreases A by 1.

Assume integer variable A holds 10 and variable B holds 20, then −

Operator Description Example
+ (Addition) Adds values on either side of the operator. A + B will give 30
- (Subtraction) Subtracts right-hand operand from left-hand operand. A - B will give -10
* (Multiplication) Multiplies values on either side of the operator. A * B will give 200
/ (Division) Divides left-hand operand by right-hand operand. B / A will give 2
% (Modulus) Divides left-hand operand by right-hand operand and returns remainder. B % A will give 0
++ (Increment) Increases the value of operand by 1. B++ gives 21
-- (Decrement) Decreases the value of operand by 1. B-- gives 19

The following programs are simple examples which demonstrate the arithmetic operators. Copy and paste the following Java programs as Test.java file, and compile and run the programs −

Example 1

In this example, we're creating two variables a and b and using arithmatic operators. We've performed addition, subtraction, multiplication and division operations and printed the results.

public class Test {

   public static void main(String args[]) {
      int a = 10;
      int b = 20;
      
      System.out.println("a + b = " + (a + b) );
      System.out.println("a - b = " + (a - b) );
      System.out.println("a * b = " + (a * b) );
      System.out.println("b / a = " + (b / a) );
   }
}

Output

a + b = 30
a - b = -10
a * b = 200
b / a = 2

Example 2

In this example, we're creating three variables a,b and c and using arithmatic operator %. We've performed Modulus operations between their values.

public class Test {

   public static void main(String args[]) {
      int a = 10;
      int b = 20;
      int c = 25;

      System.out.println("b % a = " + (b % a) );
      System.out.println("c % a = " + (c % a) );      
   }
}

Output

b % a = 0
c % a = 5

Example 3

In this example, we're creating two variables a and d and using arithmatic operators. We've performed post increment, pre increment, post decrement and post increment operations and printed the results.

public class Test {

   public static void main(String args[]) {
      int a = 10;
      int d = 25;

      System.out.println("a++   = " +  (a++) );
      System.out.println("a--   = " + (a--) );

      // Check the difference in d++ and ++d
      System.out.println("d++   = " +  (d++) );
      System.out.println("++d   = " +  (++d) );
   }
}

Output

a++   = 10
b--   = 11
d++   = 25
++d   = 27
Advertisements