CSC300: Common mistakes: Does this work? [32/37] Previous pageContentsNext page

01
02
03
04
05
06
07
08
09
10
11
  public static int numFives (double[] a) {
    return numFives (a, 0, 0);
  }
  private static int numFives (double[] a, int i, int result) {
    if (i>=a.length) 
      return result;
    if (a[i] == 5.0) 
      return numFives (a, i+1, result+1);
    else
      return numFives (a, i+1, result);
  }

Here the helper function has the same name as the starter function.

This is okay. It is called overloading in Java.

We often refer to a method by its name, but Java actually identifies the method using its name and the types of its parameters. So numFives_double[] is different from numFives_double[]_int_int.

Previous pageContentsNext page