Java Tutorial

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 List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

Java Useful Resources

Java - Arithmetic Operators with Examples



Java Arithmetic Operators

Java arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators −

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
java_basic_operators.htm
Advertisements