<< Happy New Year | Home | Friday Java Quiz: What Are These? >>

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.

Tags :


Re: Friday Java Quiz (Almost Three Days Late)

Based on some C lib docs I read, I'm going to guess "Infinity".

Re: Friday Java Quiz (Almost Three Days Late)

I expect it to compile, and hope a RuntimeException would be generated when you try to run it.

Re: Friday Java Quiz (Almost Three Days Late)

The no-compiling rule always makes me think there is something funky going on, but I believe it will compile and run without error, and will print "NaN".

Re: Friday Java Quiz (Almost Three Days Late)

I should have checked the JLS first...
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)

i think its going to compile... but throw some exception while running.

Re: Friday Java Quiz (Almost Three Days Late)

My guess is infinity. I guess that 0.0/0.0 would be NaN.

Re: Friday Java Quiz (Almost Three Days Late)

It would be less ambiguous if you just wrote: public class Foo { public static void main(String[] args) { System.out.println(Double.POSITIVE_INFINITY); } } Since they both produce identical byte code. But that's just me. :)

Re: Friday Java Quiz (Almost Three Days Late)

Regardless of what the compiler will do with 1.0/0.0, and whether that will cause a compiler error or not, I don't think that it will compile. The println method of OutputStream takes a String, and 1.0/0.0 will not produce a String.

Re: Friday Java Quiz (Almost Three Days Late)

Will the following class compile? - Yes Run without exceptions? - Yes If so, what will it print? - "Infinity"

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.

Re: Friday Java Quiz (Almost Three Days Late)

I used Scala command line, typed in 1.0/0.0 The result is: scala> 1.0/0.0 res0: Double = Infinity :)

Re: Friday Java Quiz (Almost Three Days Late)

I guess it would compile but throw a Run Time Exception when run. However if you use a final variable float f = 0.0; System.out.println(1.0/f); then you would get a compilation error.

Re: Friday Java Quiz (Almost Three Days Late)

will compile fine and print NaN I think

Re: Friday Java Quiz (Almost Three Days Late)

Even zeros are signed in IEEE754.

Add a comment Send a TrackBack