# Integer vs floating point arithmetic - Java Coding Question

I am starting a new series called Java Coding Quiz, in which I'll show you subtle Java concepts hidden in the code. This is an OCAJP or OCPJP style question but focused on teaching subtle details of Java programming language. In today's puzzle, you will learn about one of the key concepts about how floating point and integer arithmetic works in Java. This is a very important concept for any Java developer because Java behaves differently when the same operation is performed by different types of variable but of the same value.

Consider following two programs, which tests your knowledge of integer and floating point arithmetic. In first program you are dividing a double value by zero and in second program you will divide an integer value by zero.

What would be the output? will both program will print infinity or both will throw an exception or both work just fine?

First Program:

```public class Main {

public static void main(String args[]) {

// dividing a double value with zero
double value = 1;
System.out.println(value/0);

}

}```

Second program:

```public class Main {

public static void main(String args[]) {

// dividing an int value with zero
int iValue = 1;
System.out.println(iValue / 0);

}

}```

What would be the output of both program? Will the both print INFINITY, throw an exception or will not compile?

Our first program will print Infinity but our second program will throw ArithmeticException as shown below:

Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Main.main(Main.java:7)

## Explanation

Even though both programs looks similar there is a huge difference between the two operations. In the first program, we have divided a double value by zero, which produces Infinity because it uses floating point arithmetic. In the first step, 0 is promoted to 0.0 and 1.0/0.0 is Infinity because of its legal in floating point arithmetic as per Java specification. Also, Infinity is a value double value i.e. Double.INFINITY.

On the second program, we have divided an integer value by zero, which results in ArithmaticException because divide by zero is illegal as per integer arithmetic in Java. In this program, no promotion happens and both 1 and 0 are treated as an int, hence ArithmaticException is thrown.

In short, you should always pay attention to the type of value in any expression. Even same operation with the same value but different data type can produce different results as it happens in this coding question. These are a very subtle detail but quite useful. You often hit by those accidental but knowing the right reason always helps to avoid these kinds of nasty errors.

Tip 1: You can further check my post 10 tricky question in Java for more of such questions, I have shared several tricky questions from Java interviews on that post.

Tip 2: You can also read Java Puzzlers to learn such corner cases of Java programming. It's written by Joshua Bloch and Neal Gafter and one of the interesting Java book for experienced developers. I truly test your knowledge of Java and how much you need to learn.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

Prabjot said...

Why java doesn't give infinity in case if integer ?

Unknown said...

Why it will give -1 as output in this condition
double value = 255;
byte c= (byte) (value/1);
System.out.println(c);

Javin Paul said...

@Prabjot, because result of integer arithmetic is Integer and INFINITY cannot be integer, it's only defined for double in Java.

Javin Paul said...

@Unknown, because when you cast 255 to byte it can only held the last 8 bytes, rest is lost. Since Java is signed system, in order to represent 255 you need 9 digits e.g. 0 1111 1111 so when you cast you got 1111 1111 which is -1

S A E D A W K E said...

@Jabin Paul. How to follow Java Coding Quiz, in the future. Does it possible to subscribe in order to get updates. Did you placed seperate category.

Me mostly I follows from G+ notification.

Javin Paul said...

Hello @Saedawke, you can subscribe to the RSS feed of this blog or you can join Javarvisited facebook page https://www.facebook.com/javarevisited/ to get updates.