Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

public class Program20
{
    public static void main(String... args)
    {
        double negZero = -0.0;
        double posZero = 0.0;
        System.out.println(Math.min(negZero, posZero));
    }
}

  • A.The program prints 0
  • B.Compilation fails.
  • C.The program prints 0.0
  • D.The program prints -0.0

Answer: D

Math.min for doubles is specified to treat negative zero as strictly smaller than positive zero, so it returns -0.0 and println prints the sign. The primitive comparison -0.0 == 0.0 is true, which is why this looks surprising: the two are equal but not identical.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.