# Concept Of Infinity in Java Arithmetic Exception vs Display of Infinity

Have you ever heard of infinity concept in java? if no, this post is for you.

In mathematics x/y =infinity when y=0

in our normal computation there is no way to show infinity. Normal computation means when we use integral arithmetic(data type-int,short,char,byte,long)

So any of these datatype's variable divided by zero , we are going to get

Mostly

Let's see how..

Exception in thread "main" java.lang.ArithmeticException: / by zero at operatortest.main(operatortest.java:6)

But when we will use double and float point arithmetic computation, we have a way to represent infinity.

In mathematics we can have positive infinity and negative infinity. Similarly in java computation we can have positive infinity and negative infinity.

The above written code produces positive infinity.

So please remember any number divided by zero does not necessarily give arithmetic exception. There can be infinity too.In mathematics x/y =infinity when y=0

in our normal computation there is no way to show infinity. Normal computation means when we use integral arithmetic(data type-int,short,char,byte,long)

So any of these datatype's variable divided by zero , we are going to get

**Arithmetic exception ::divided by zero error**.Mostly

Let's see how..

public class operatortest { public static void main(String[] Args) { int x=10; System.out.print(x/0); } }

**Exact Error code:**

Exception in thread "main" java.lang.ArithmeticException: / by zero at operatortest.main(operatortest.java:6)

But when we will use double and float point arithmetic computation, we have a way to represent infinity.

public class operatortest { public static void main(String[] Args) { double x=10.00; System.out.print(x/0); } }output: Infinity

In mathematics we can have positive infinity and negative infinity. Similarly in java computation we can have positive infinity and negative infinity.

The above written code produces positive infinity.

public class operatortest { public static void main(String[] Args) { double x=-10.00; System.out.print(x/0); } }output: - Infinity

The corresponding definition can be found here

**o POSITIVE_INFINITY**

public final static double POSITIVE_INFINITY

Positive infinity.

**o NEGATIVE_INFINITY**

public final static double NEGATIVE_INFINITY

Negative infinity.

So for integral arithmetic we will get Run time Error of Divide by Zero but for floating point arithmetic we will get positive or negative infinity rather than getting Run time error.

## Post a Comment