Integer Vs Floating Betoken Arithmetics - Coffee Coding Question

I am starting a novel serial called Java Coding Quiz, inwards which I'll present y'all subtle Java concepts hidden inwards the code. This is an OCAJP or OCPJP fashion inquiry but focused on pedagogy subtle details of Java programming language. In today's puzzle, y'all volition larn nearly i of the key concepts nearly how floating signal in addition to integer arithmetics industrial plant inwards Java. This is a real of import concept for whatever Java developer because Java behaves differently when the same performance is performed yesteryear dissimilar types of variable but of the same value.


Consider next 2 programs, which tests your noesis of integer in addition to floating signal arithmetic. In commencement computer program y'all are dividing a double value yesteryear nothing in addition to inwards mo computer program y'all volition separate an integer value yesteryear zero.

What would endure the output? volition both computer program volition impress infinity or both volition throw an exception or both operate only fine?

First Program:

public class Main {  public static void main(String args[]) {  // dividing a double value amongst zero double value = 1; System.out.println(value/0);  }  }




Second program:

public class Main {  public static void main(String args[]) {  // dividing an int value amongst zero int iValue = 1; System.out.println(iValue / 0);  }  }


What would endure the output of both program? Will the both impress INFINITY, throw an exception or volition non compile?




Answer

Our commencement computer program volition impress Infinity but our mo computer program volition throw ArithmeticException every bit shown below:

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


Explanation

Even though both programs looks like in that place is a huge divergence betwixt the 2 operations. In the commencement program, nosotros convey divided a double value yesteryear zero, which produces Infinity because it uses floating signal arithmetic. In the commencement step, 0 is promoted to 0.0 in addition to 1.0/0.0 is Infinity because of its legal inwards floating signal arithmetics every bit per Java specification. Also, Infinity is a value double value i.e. Double.INFINITY.


On the mo program, nosotros convey divided an integer value yesteryear zero, which results inwards ArithmaticException because divide yesteryear nothing is illegal every bit per integer arithmetics inwards Java. In this program, no advertisement happens in addition to both 1 in addition to 0 are treated every bit an int, thence ArithmaticException is thrown.

In short, y'all should ever pay attending to the type of value inwards whatever expression. Even same performance amongst the same value but dissimilar information type tin arrive at dissimilar results every bit it happens inwards this coding question. These are a real subtle exceptional but quite useful. You oftentimes striking yesteryear those accidental but knowing the correct argue ever helps to avoid these kinds of nasty errors.

Tip 1: You tin farther banking concern fit my postal service 10 tricky inquiry inwards Java for to a greater extent than of such questions, I convey shared several tricky questions from Java interviews on that post.


Tip 2: You tin too read Java Puzzlers to larn such corner cases of Java programming. It's written yesteryear Joshua Bloch in addition to Neal Gafter in addition to i of the interesting Java mass for experienced developers. I genuinely assay your noesis of Java in addition to how much y'all necessitate to learn.

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

0 Response to "Integer Vs Floating Betoken Arithmetics - Coffee Coding Question"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel