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.

The 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.

 
public class Main {

  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:


Integer Arithmetic
a = 2
b = 6
c = 1
d = -1
e = 1

Floating Point Arithmetic

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

 
public class Main {
  public static void main(String args[]) {
    int x = 42;

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

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


x mod 10 = 2

The modulus operator can be applied to floating-point types as well as integer types.

 
public class Main {
  public static void main(String args[]) {
    double y = 42.25;

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

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


y mod 10 = 2.25

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;
          break;
        }
      }
      if (isPrime)
        System.out.print(i + " ");
    }
  }
}
  

The output:


Prime numbers between 1 and 100
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

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 {

  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:


a = 2
b = 4
c = 2

Increment and Decrement

  • ++ 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.

Examples of Pre-and Post- Increment and Decrement Operations

Initial Value of xExpressionFinal Value of yFinal Value of x
5y = x++56
5y = ++x66
5y = x--54
5y = --x44

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 {

  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:


a = 2
b = 3
c = 3
d = 1
Home 
  Java Book 
    Language Basics  

Operators:
  1. Operators
  2. Arithmetic Operators
  3. Bitwise Operators
  4. Relational Operators
  5. Boolean Logical Operators
  6. The ? Operator
  7. Operator Precedence