Using overloaded methods to print array of different types : Method Overloading « Class Definition « Java Tutorial






public class MainClass {
  // method printArray to print Integer array
  public static void printArray(Integer[] inputArray) {
    // display array elements
    for (Integer element : inputArray)
      System.out.printf("%s ", element);
    System.out.println();
  }
  // method printArray to print Double array
  public static void printArray(Double[] inputArray) {
    // display array elements
    for (Double element : inputArray)
      System.out.printf("%s ", element);
    System.out.println();
  }

  // method printArray to print Character array
  public static void printArray(Character[] inputArray) {
    // display array elements
    for (Character element : inputArray)
      System.out.printf("%s ", element);

    System.out.println();
  }

  public static void main(String args[]) {
    // create arrays of Integer, Double and Character
    Integer[] integerArray = { 1, 2, 3, 4, 5, 6 };
    Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
    Character[] characterArray = { 'H', 'E', 'L', 'L', 'O' };

    System.out.println("Array integerArray contains:");
    printArray(integerArray); // pass an Integer array
    System.out.println("\nArray doubleArray contains:");
    printArray(doubleArray); // pass a Double array
    System.out.println("\nArray characterArray contains:");
    printArray(characterArray); // pass a Character array
  }
}
Array integerArray contains:
1 2 3 4 5 6 

Array doubleArray contains:
1.1 2.2 3.3 4.4 5.5 6.6 7.7 

Array characterArray contains:
H E L L O








5.5.Method Overloading
5.5.1.Method Overloading
5.5.2.Using Method Overloading
5.5.3.Pass long parameters to overloading method
5.5.4.Primitives and overloading
5.5.5.Overloading based on the order of the arguments
5.5.6.Demonstration of both constructor and ordinary method overloading
5.5.7.Using overloaded methods to print array of different types
5.5.8.Methods with differing type signatures are overloaded - not overridden.