What does the following expression return in Java?
Math.max(Float.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
I saw this question in a website and the answer is Double.POSITIVE_INFINITY. I’m not sure about this answer as how can we compare 2 infinities? Can someone clarify this? Thanks.
Float.POSITIVE_INFINITY returns float and Double.POSITIVE_INFINITY returns double.
There is no method called Math.max(float, double). only Math.max(float, float) and Math.max(double, double)
Therefore when the method is called Math.max(float, double), it converts the float argument to double and so the Math.max(double, double) is called so Double.POSITIVE_INFINITY is returned.
Java does not convert from double to float since it may lead to precision problem.