Why isn't Math.max(double a, double b) variadic?

前端 未结 6 957
逝去的感伤
逝去的感伤 2021-01-12 23:33

Why isn\'t the implementation of Math.max a variadic function?

It could get implemented like this:

public class Main {
    public static double max(d         


        
6条回答
  •  难免孤独
    2021-01-13 00:11

    Math.max has been around since JDK 1.0, long before the variable # of argument syntax was introduced. This is not to say that the method could not be updated the way you suggest. Sometimes library method definitions or implementations are changed, but this is rare. Most of the time new methods are added to classes rather than modifying existing methods.

    Your new implementation of Max is really a case of method overloading because it is possible for the existing method and the new var args method to exist side by side in the same class. So while it can certainly replace the existing method, it could also just be an addition to the Math class. So I think it should be added. The fact that we can leave the existing method alone removes any concern about performance that the new implementation might raise.

    The culture of what can get changed between Java n and Java n+1 is changing anyway. For instance, the File access classes and java.sql.Connection were changed from Java 6 to Java 7 because in Java 7 they now implement AutoCloseable. Java 9 is actually going to remove some methods from classes that are in the way of project jigsaw.

    I can think of no valid reason for Math.max not being updated. Maybe no one suggested it until now. Are you reading this, Mark Reinhold?

提交回复
热议问题