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

前端 未结 6 875
逝去的感伤
逝去的感伤 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:09

    The java.lang.Math has been introduced in JDK 1.0, long before variadic functions were introduced into the language in Java 5.

    In addition, efficiency is a concern: if you need two elements most of the time, it is much faster to pass them "inline", without creating an intermediate array to hold them. This also avoids the costs of setting up a loop inside the implementation.

提交回复
热议问题