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 the 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 variables but of the same value.
Consider the following two programs, which test your knowledge of integer and floating-point arithmetic. In the first program, you are dividing a double value by zero and in the second program, you will divide an integer value by zero.
What would be the output? will both programs will print infinity or both will throw an exception or both work just fine?
First Program:
Second program:
What would be the output of both programs? Will both print INFINITY, throw an exception, or will not compile?
You may have guessed it correctly but if not don't worry, I will explain the logic next, And, if you are new to the Java world then I also recommend you go through The Complete Java MasterClass on Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.
Consider the following two programs, which test your knowledge of integer and floating-point arithmetic. In the first program, you are dividing a double value by zero and in the second program, you will divide an integer value by zero.
What would be the output? will both programs 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 programs? Will both print INFINITY, throw an exception, or will not compile?
Answer
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)
You may have guessed it correctly but if not don't worry, I will explain the logic next, And, if you are new to the Java world then I also recommend you go through The Complete Java MasterClass on Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.
Explanation
Even though both programs look 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 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 the same operation with the same value but different data types can produce different results as it happens in this coding question. These are very subtle detail but quite useful. You are often hit by those accidents but knowing the right reason always helps to avoid these kinds of nasty errors.
Tip 1: You can further check my post 10 tricky questions 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.
Why java doesn't give infinity in case if integer ?
ReplyDeleteWhy it will give -1 as output in this condition
ReplyDeletedouble value = 255;
byte c= (byte) (value/1);
System.out.println(c);
@Prabjot, because result of integer arithmetic is Integer and INFINITY cannot be integer, it's only defined for double in Java.
ReplyDelete@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
ReplyDelete@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.
ReplyDeleteMe mostly I follows from G+ notification.
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.
ReplyDelete