Perform Divide by zero on different data types of Java
UPDATED: 11 October 2015
Tags:
J2SE
60% - 70% Java developer thinks that if you divide any number with zero '0' gives you java.lang.ArithmeticException: / by zero
short / Short / int / Integer / long / Long
When you perform divide by zero (0) on mentioned data types then it will give you java.lang.ArithmeticException: / by zero irrespective of Positive Number, Negative Number or even Zero(0).
float / Float / double / Double
When you perform divide by zero (0 or 0.0) on mentioned data type then it will give you different result based on Positive Number, Negative Number and Zero(0).
- Positive Number / 0 or Positive Number / 0.0 - Gives Infinity
- Negative Number / 0 or Negative Number / 0.0 - Gives -Infinity
- 0 / 0.0 or 0.0 / 0 - Gives Nan (Not A Number)
Source code
public class DivideByZeroExample {
public static void main(String[] args) {
try {
/**
* All below operation will give you
* 'java.lang.ArithmeticException: / by zero'
*/
short a = 111;
short a1 = 0;
System.out.println(a/0);
//System.out.println(a1/0);
Short x = 123;
Short x1 = 0;
//System.out.println(x/0);
//System.out.println(x1/0);
int b = 222;
int b1 = 0;
//System.out.println(b/0);
//System.out.println(b1/0);
Integer y = 234;
Integer y1 = 0;
//System.out.println(y/0);
//System.out.println(y1/0);
long c = 333;
long c1 = 0;
//System.out.println(c/0);
//System.out.println(c1/0);
Long z = 345L;
Long z1 = 0L;
//System.out.println(z/0);
//System.out.println(z1/0);
} catch (ArithmeticException e) {
e.printStackTrace();
}
/* Performing Divide by zero on floating data types */
float d = 111;
float d1 = 0;
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
System.out.println(d + "/0" + " , " + -d +"/0 [float(Positive/(-)Negative) Divide by zero gives (Infinity/-Infinity)]: " + d/0 + " , " + -d/0);
System.out.println(d1 + "/0" + " [float '0.0' Divide by zero gives 'Nan' (Not A Number)]: " + d1/0);
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
Float e = 123F;
Float e1 = 0F;
System.out.println(e + "/0" + " , " + -e +"/0 [Float(Positive/(-)Negative) Divide by zero gives (Infinity/-Infinity)]: " + e/0 + " , " + -e/0);
System.out.println(e1 + "/0" + " [Float '0.0' Divide by zero gives 'Nan' (Not A Number)]: " + e1/0);
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
double m = 111;
double m1 = 0;
System.out.println(m + "/0" + " , " + -m +"/0 [double(Positive/(-)Negative) Divide by zero gives (Infinity/-Infinity)]: " + m/0 + " , " + -m/0);
System.out.println(m1 + "/0" + " [double '0.0' Divide by zero gives 'Nan' (Not A Number)]: " + m1/0);
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
Double n = 123D;
Double n1 = 0D;
System.out.println(n + "/0" + " , " + -n +"/0 [Double(Positive/(-)Negative) Divide by zero gives (Infinity/-Infinity)]: " + n/0 + " , " + -n/0);
System.out.println(n1 + "/0" + " [Double '0.0' Divide by zero gives 'Nan' (Not A Number)]: " + n1/0);
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
}
}
Output
java.lang.ArithmeticException: / by zero at com.javaquery.math.DivideByZeroExample.main(DivideByZeroExample.java:22) +++++++++++++++++++++++++++++++++++++++++++++ 111.0/0 , -111.0/0 [float(Positive/(-)Negative) Divide by zero gives (Infinity/-Infinity)]: Infinity , -Infinity 0.0/0 [float '0.0' Divide by zero gives 'Nan' (Not A Number)]: NaN +++++++++++++++++++++++++++++++++++++++++++++ 123.0/0 , -123.0/0 [Float(Positive/(-)Negative) Divide by zero gives (Infinity/-Infinity)]: Infinity , -Infinity 0.0/0 [Float '0.0' Divide by zero gives 'Nan' (Not A Number)]: NaN +++++++++++++++++++++++++++++++++++++++++++++ 111.0/0 , -111.0/0 [double(Positive/(-)Negative) Divide by zero gives (Infinity/-Infinity)]: Infinity , -Infinity 0.0/0 [double '0.0' Divide by zero gives 'Nan' (Not A Number)]: NaN +++++++++++++++++++++++++++++++++++++++++++++ 123.0/0 , -123.0/0 [Double(Positive/(-)Negative) Divide by zero gives (Infinity/-Infinity)]: Infinity , -Infinity 0.0/0 [Double '0.0' Divide by zero gives 'Nan' (Not A Number)]: NaN +++++++++++++++++++++++++++++++++++++++++++++
Tags:
J2SE

0 comments :