Java - Operator Division Operator /

What is Division Operator?

The division operator (/) is used in the form

operand1 / operand2 
  

The division operator computes the quotient of two numbers.

Types of division

There are two types of division in Java:

  • Integer division
  • Floating-point division

Integer division

If both the operands of the division operator are integers, that is, byte, short, char, int, or long, the division is done and decimal part is removed to represent an integer.

For example, 5/2 yields 2.5; the fractional part 0.5 is ignored; and the result is 2.

The following examples illustrate the integer division:

  
int num; 
num = 5/2; // Assigns 2 to num 
num = 5/3; // Assigns 1 to num 
num = 5/4; // Assigns 1 to num 
num = 5/5; // Assigns 1 to num 
num = 5/6; // Assigns 0 to num 
num = 5/7; // Assigns 0 to num 

Demo

public class Main {
  public static void main(String[] args) {
    int num; // w ww. ja v a2s.  co  m
    num = 5/2; // Assigns 2 to num 
    System.out.println(num);
    num = 5/3; // Assigns 1 to num
    System.out.println(num);
    num = 5/4; // Assigns 1 to num
    System.out.println(num);
    num = 5/5; // Assigns 1 to num
    System.out.println(num);
    num = 5/6; // Assigns 0 to num
    System.out.println(num);
    num = 5/7; // Assigns 0 to num 
    System.out.println(num);
  }
}

Result

Floating-point division

If either of the operands are float or double type, floating-point division is performed and the result is not truncated.

Demo

public class Main {
  public static void main(String[] args) {
    float f1; //from  w w  w.  j  av  a  2s.  c o m
    f1 = 15.0F/4.0F;
    System.out.println(f1);
    f1 = 15/4.0F; 
    System.out.println(f1);
  }
}

Result

Division Operator Promotion

In the following code, 15.0 is of the type double and 4.0F is of the type float. The expression 15.0/4.0F is of type double. The result 3.75 is of the type double and cannot be assigned to f1.

f1 = 15.0/4.0F; // An error.

To fix the error, add casting

f1 = (float)(15.0/4.0F); // Ok. 3.75F is assigned to f1 

Integer division and float

In the following code, 15 and 4 are of the type int. The expression 15/4 is of type int. An integer division is performed. The result 3 is assigned to f1. int to float assignment is allowed.

float f1 = 15/4; 

Demo

public class Main {
  public static void main(String[] args) {
    float f1; /*ww  w  .j  a  v  a  2s  . c  o  m*/
    f1 = 15/4; 
    System.out.println(f1);
  }
}

Result

Divide by 0

The result of dividing a number by zero depends on the type of division.

If an integer division is performed on the number, dividing by zero results in a runtime error.

If you write expression 3/0 in a Java program, it compiles fine, but it gives error when it is executed at runtime. For example,

  
int i = 2; 
int j = 5; 
int k = 0; 
i = j/k;  // A runtime error. Divide by zero 
i = 0/0;  // A runtime error. Divide by zero 

Demo

public class Main {
  public static void main(String[] args) {
    int i = 2; //from   ww w. j a va 2 s.  c  o m
    int j = 5; 
    int k = 0; 
    i = j/k;  // A runtime error. Divide by zero 
    i = 0/0;  // A runtime error. Divide by zero 
    System.out.println(i);
  }
}

Result

If either operand of the division operator is a floating-point number, a floating-point division is performed.

The result of dividing by zero is not an error. If the dividend is a non-zero number in a floating-point divide-by-zero operation, the result is either positive infinity or a negative infinity.

If the dividend is a floating-point zero (e.g. 0.0 or 0,0F), the result is NaN. For example,

  
float f1 = 0F; 
double d1 = 0.0; 
  
f1 = 5.0F/0.0F;    // Float.POSITIVE_INFINITY is assigned to f1 
f1 = -5.0F/0.0F;   // Float.NEGATIVE_INFINITY is assigned to f1 
f1 = -5.0F/-0.0F;  // Float.POSITIVE_INFINITY is assigned to f1 
f1 = 5.0F/-0.0F;   // Float.NEGATIVE_INFINITY is assigned to f1 
d1 = 5.0/0.0;      // Double.POSITIVE_INFINITY is assigned to d1 
d1 = -5.0/0.0;     // Double.NEGATIVE_INFINITY is assigned to d1 
d1 = -5.0/-0.0;    // Double.POSITIVE_INFINITY is assigned to d1 
d1 = 5.0/-0.0;     // Double.NEGATIVE_INFINITY is assigned to d1 

Demo

public class Main {
  public static void main(String[] args) {
    float f1 = 0F; 
    double d1 = 0.0; 
      /* ww  w. ja v  a 2  s  .  co m*/
    f1 = 5.0F/0.0F;    // Float.POSITIVE_INFINITY is assigned to f1
    System.out.println(f1);
    f1 = -5.0F/0.0F;   // Float.NEGATIVE_INFINITY is assigned to f1 
    System.out.println(f1);
    f1 = -5.0F/-0.0F;  // Float.POSITIVE_INFINITY is assigned to f1
    System.out.println(f1);
    f1 = 5.0F/-0.0F;   // Float.NEGATIVE_INFINITY is assigned to f1
    System.out.println(f1);
    d1 = 5.0/0.0;      // Double.POSITIVE_INFINITY is assigned to d1
    System.out.println(d1);
    d1 = -5.0/0.0;     // Double.NEGATIVE_INFINITY is assigned to d1
    System.out.println(d1);
    d1 = -5.0/-0.0;    // Double.POSITIVE_INFINITY is assigned to d1
    System.out.println(d1);
    d1 = 5.0/-0.0;     // Double.NEGATIVE_INFINITY is assigned to d1 
    System.out.println(d1);
  }
}

Result

NAN

f1 = 0.0F/0.0F;  // Assigns Float.NaN to f1 
d1 = 0.0/0.0;    // Assigns Double.NaN to d1 
d1 = -0.0/0.0;   // Assigns Double.NaN to d1 

Demo

public class Main {
  public static void main(String[] args) {
    float f1 = 0.0F/0.0F;  // Assigns Float.NaN to f1 
    System.out.println(f1);/*from w  w w  .  j av  a2  s. com*/
    double d1 = 0.0/0.0;    // Assigns Double.NaN to d1
    System.out.println(d1);
    d1 = -0.0/0.0;   // Assigns Double.NaN to d1 
    System.out.println(d1);
  }
}

Result

Division Operator Promotion

In the following code, 5.0F is of the type float and 0 is of the type int. 5.0F/0 is of type float. Float.POSITIVE_INFINITY is assigned to f1

float f1 = 5.0F/0; 

Demo

public class Main {
  public static void main(String[] args) {
    float f1 = 5.0F/0; 
    System.out.println(f1);/*from w  w  w  .j  ava2s .com*/
  }
}

Result

In the following code, 5.0F is of the type float and 0.0 is of the type double 5.0F/0.0 is of the type double. double to float assignment is not allowed.

float f1 = 5.0F/0.0; // A compile-time error.

To fix the error, add casting.

float f1 = (float)(5.0F/0.0); // f1 is assigned Float.POSITIVE_INFINITY