Java Arithmetic Operators

Introduction

Java arithmetic operators are used in mathematical expressions in the same way that they are used in math.

The following table lists the arithmetic operators:

Operator Result
+ Addition and unary plus
- Subtraction and 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 can use them on char types, since the char type in Java is a subset of int.

Basic Arithmetic Operators

The basic arithmetic operations-addition, subtraction, multiplication, and division-behave the same as algebra.

The unary minus operator negates its single operand.

The unary plus operator returns the value of its operand.

When the division operator is used on an integer type, there will be no fractional component returned.

The following simple example program demonstrates the arithmetic operators.


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;/*  ww  w.  java  2 s  .  com*/
    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);
  }
}

The following code illustrates the difference between floating-point division and integer division.

public class Main {
  public static void main(String args[]) {
    // arithmetic using integers
    System.out.println("Integer Arithmetic");
    int a = 1 + 1;
    int b = a * 3;
    int c = b / 4;
    int d = c - a;
    int e = -d;//  w  w  w .  j  a v a2s . c om
    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);

    // arithmetic using doubles
    System.out.println("\nFloating Point Arithmetic");
    double da = 1 + 1;
    double db = da * 3;
    double dc = db / 4;
    double dd = dc - a;
    double de = -dd;
    System.out.println("da = " + da);
    System.out.println("db = " + db);
    System.out.println("dc = " + dc);
    System.out.println("dd = " + dd);
    System.out.println("de = " + de);
  }
}



PreviousNext

Related