Solution to compiler warning for generic varargs

Posted by TJR on Stack Overflow See other posts from Stack Overflow or by TJR
Published on 2011-11-21T01:45:27Z Indexed on 2011/11/21 1:51 UTC
Read the original article Hit count: 130

Filed under:
|

A puzzle from this blog. Similar to SO1445233.

Given the following source listing, explain why the compiler is producing a warning at invocation to the list method and give a solution for removing the warning without resorting to @SuppressWarnings annotation.

public class JavaLanguagePuzzle3 {
  public static void main(String[] args) {
    list("1", 2, new BigDecimal("3.5"));
  }  
  private static <T> List<T> list(T... items) {
    return Arrays.asList(items);
  }
}

Warning:

Type safety: A generic array of Object&Serializable&Comparable<?> is created for a varargs parameter

© Stack Overflow or respective owner

Related posts about java

Related posts about eclipse