Friday Java Quiz (Almost Three Days Late)
Q: Will the following class compile? Run without exceptions? If so, what will it print?
1 public class Foo { 2 public static void main(String[] args) { 3 System.out.println(1.0/0.0); 4 } 5 }
Strict rules: no compiling.
Re: Friday Java Quiz (Almost Three Days Late)
I should have checked the JLS first...
From the JLS third ed, Section 15.17.2;
So by the second rule, the output would be "Infinity", not "NaN" as I thought in my previous post.
From the JLS third ed, Section 15.17.2;
"Division of a zero by a zero results in NaN..."
"Division of a nonzero finite value by a zero results in a signed infinity..."
So by the second rule, the output would be "Infinity", not "NaN" as I thought in my previous post.
Re: Friday Java Quiz (Almost Three Days Late)
@Mike P
Sorry println is not part of OutputStream. PrintStream.println() has quite a few definitions, one for each primitive, one for String and finally object. There is no compile time error associated with the type returned by the divide by zero. I think Mike Heath hit it on the head.