spring-el

Implicit conversion from int[] to double[]


I have two functions:

    public static double avg(int[] values) {
        if(values == null || values.length == 0) return -1;
        double sum = 0;

        for(int value:values) {
            sum = sum + value;
        }
        return (sum/values.length);
    }

    public static double avg(double[] values) {
        if(values == null || values.length == 0) return -1;
        double sum = 0;

        for(double value:values) {
            sum = sum + value;
        }
        return (sum/values.length);
    }

I am using the SpEL expression engine to evaluate the expressions. When I deploy this code and invoke the expression avg({3,4,5}) or avg({3.0,4.0,5.0}), I get the below error:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1033E:(pos  0): Method call of 'avg' is ambiguous, supported type conversions allow multiple variants to match

Does int[] array implicitly get converted into double[] during evaluation?

Shall I make it a single function avg(double[] values)?


Solution

  • {3,4,5} can be interpreted as array of ints or as array of doubles. use double arr[] = {3,4,5}; avg(arr); or int arr[] = {3,4,5}; avg(arr); and tou should have no errors