0

In the following scenario I have a method which accepts 2 arrays of Type E. It was my understanding that this E parameter means that both the arrays can be any type, but they must be the same (as I derived from this question). I've tested this with two arrays, one Integer and one Double, but I'm not receiving any error. The output I receive is '14' which is the combined size of both arrays without any error thrown.

Could someone shed some light as to why this works?

public static <E> void showCombinedLength(E[] array1, E[] array2){
    System.out.println(array1.length + array2.length);
}

public static void main(String[] args) {
    Integer[] integerArray = {1, 2, 3, 4, 5, 6, 7};
    Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
    PrintArray.showCombinedLength(integerArray, doubleArray);
}

Output:

14

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170

3 Answers3

4

When you define a single generic type in the method definition then you can only use the method with two arrays of the same type.

However, in java, array types are covariant, so both Integer[] and Double[] are subclasses of Object[] (they are also subclasses of Number[]). So your code will always compile, and there is no need to specify multiple type parameters.

Tom McIntyre
  • 3,620
  • 2
  • 23
  • 32
  • 2
    In fact, there is no need to specify any type parameters at all. It would work the same as if he declared it `public static void showCombinedLength(Object[] array1, Object[] array2)` – newacct Jan 06 '13 at 21:56
2

Your arrays are both arrays of objets. Call it with

PrintArray.<Integer>showCombinedLength(integerArray, doubleArray);

and the compiler will refuse to compile.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Would you mind telling me what the name of the notation is? As far as I can tell from the showing error, it means the method that follows can only accept Integer parameters. I'd like to read up on this notation. – Jeroen Vannevel Jan 06 '13 at 14:52
  • Calling generic method with explicit parameter types. – Adam Stelmaszczyk Jan 06 '13 at 14:58
  • 1
    If you don't add ``, you let the compiler infer the generic parameter type to use from the type of the arguments of the method. So the compiler infers Number[] or Object[] here. When you don't want that, and explicitely want a specific parameter type, you need to specify it as I did. This is useful when, for example, you want to pass an empty list of Foo and the compiler infers a List instead: try doing `String s = foo(Collections.emptyList());` where the method is defined as `public static T foo(List list)`. It doesn't compile. Pass `Collections.emptyList()`. – JB Nizet Jan 06 '13 at 15:07
-1

Generics were introduced to Java in version 1.5. Due to the backward compability Java byte code doesn't know that some method is generic and another not. This is why after compilation your method really is:

public static void showCombinedLength(Object[] array1, Object[] array2)

All generic types are really changed to Object. So both Integer[] and Double[] are really arrays of objects which have length method. This is why this code works.

Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60