Java Arithmetic Operators

In this chapter you will learn:

  1. Use Java Arithmetic Operators to do calculation
  2. How to use basic arithmetic operators to do calculation
  3. How to use modulus operator to get the remainder of a division operation
  4. How to use arithmetic compound assignment operators
  5. How to use Java increment and decrement operators

All Arithmetic Operators

Arithmetic operators are used in mathematical expressions. The following table lists the arithmetic operators:

OperatorResult
+ Addition
- Subtraction (unary minus)
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
-- Decrement

The operands of the arithmetic operators must be of a numeric type. You cannot use arithmetic operators on boolean types, but you can use them on char types.

Basic Arithmetic Operators

The basic arithmetic operations are addition, subtraction, multiplication, and division. They behave as you would expect. The minus operator also has a unary form which negates its single operand.

The quick demo below shows how to do a simple calculation in Java with basic arithmetic operators.

public class Main {
//from  j a v a 2  s  .  co m
  public static void main(String args[]) {

    System.out.println("Integer Arithmetic");
    int a = 1 + 1;
    int b = a * 3;
    int c = b / 4;
    int d = c - a;
    int e = -d;
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);
    System.out.println("d = " + d);
    System.out.println("e = " + e);

  }
}

When you run this program, you will see the following output:

Java Modulus operator

The modulus operator, %, returns the remainder of a division operation.

public class Main {
  public static void main(String args[]) {
    int x = 42;/*from   j  av a2s.  co  m*/

    System.out.println("x mod 10 = " + x % 10);
  }
}

When you run this program you will get the following output:

The modulus operator can be applied to floating-point types as well as integer types. The following code uses the modulus operator on a double type variable.

public class Main {
  public static void main(String args[]) {
    double y = 42.25;
/*j  av  a 2  s.  c o  m*/
    System.out.println("y mod 10 = " + y % 10);
  }
}

When you run this program you will get the following output:

The following code uses the modulus operator to calculate the prime numbers:

public class Main {
  public static void main(String[] args) {
    int limit = 100;
    System.out.println("Prime numbers between 1 and " + limit);
    for (int i = 1; i < 100; i++) {
      boolean isPrime = true;
      for (int j = 2; j < i; j++) {
        if (i % j == 0) {
          isPrime = false;// j  av  a  2s .  co  m
          break;
        }
      }
      if (isPrime)
        System.out.print(i + " ");
    }
  }
}

The output:

Arithmetic Compound Assignment Operators

Statements like the following

a = a + 4;

can be rewritten as

a += 4;

Both statements perform the same action: they increase the value of a by 4.

Here is another example,

a = a % 2;

which can be expressed as

a %= 2;

In this case, the %= obtains the remainder of a/2 and puts that result back into a. Any statement of the form

var = var op expression;

can be rewritten as

var op= expression;

Here is a sample program that shows several op= operator assignments:

public class Main {
//jav  a2 s  . com
  public static void main(String args[]) {
    int a = 1;
    int b = 2;
    int c = 3;

    a += 1;
    b *= 2;
    c += a * b;
    c %= 3;
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);

  }
}

The output of this program is shown here:

Increment and Decrement Operator

++ and -- are Java's increment and decrement operators. The increment operator, ++, increases its operand by one. The decrement operator, --, decreases its operand by one.

For example, this statement:

x = x + 1;

can be rewritten like this by use of the increment operator:

x++;

This statement:

x = x - 1;

is equivalent to

x--;

The increment and decrement operators are unique in that they can appear both in postfix form and prefix form. In the postfix form they follow the operand, for example, i++. In the prefix form, they precede the operand, for example, --i.

The difference between these two forms appears when the increment and/or decrement operators are part of a larger expression. In the prefix form, the operand is incremented or decremented before the value is used in the expression. In postfix form, the value is used in the expression, and then the operand is modified.

The following table summarizes the difference between Pre-and Post- Increment and Decrement Operations:

Initial Value of xExpressionFinal Value of yFinal Value of x
5 y = x++ 5 6
5 y = ++x 6 6
5 y = x-- 5 4
5 y = --x 4 4

For example:

x = 42; 
y = ++x;

y is set to 43, because the increment occurs before x is assigned to y. Thus, the line

y = ++x;

is the equivalent of these two statements:

x = x + 1; 
y = x;

However, when written like this,

x = 42; 
y = x++;

the value of x is obtained before the increment operator is executed, so the value of y is 42.

In both cases x is set to 43. The line

y = x++;

is the equivalent of these two statements:

y = x; 
x = x + 1;

The following program demonstrates the increment operator.

public class Main {
/*from   j a v  a2 s .  c  om*/
  public static void main(String args[]) {
    int a = 1;
    int b = 2;
    int c = ++b;
    int d = a++;

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

  }
}

The output of this program follows:

Next chapter...

What you will learn in the next chapter:

  1. What are bitwise operation
  2. How to shift bits in a value to the left
  3. How to shift bits in a value to the right
  4. How to use unsigned right shift
  5. How to use bitwise operator assignments
  6. How to use the Bitwise Logical Operators
Home » Java Tutorial » Operators Statements
Your first Java program
Your second Java program
Java Keywords and Identifiers
Variable stores value in a Java program
Variable Scope and Lifetime
Java Operators
Java Arithmetic Operators
Java Bitwise Operators
Java Relational Operators
Java Boolean Logical Operators
Java If Statement
Java switch Statement
Java while Loop
Java for loop
Java for each loop
Java break statement
Java continue statement
Java return statement
Java Comments
Java documentation comment(Javadoc)