0

I have a list of an ArrayList:

public class Ex{
  public static void main(String[] args){

   List<Object> list = new ArrayList<>();

    list.add(1L); 

    list.add("hello");

      List<?> list1 = new ArrayList<>();

      list1.add(1L);

      list1.add("hello");
   }
}

Why I can't add 1L , and "hello" String to the unbound wildcard ?

Labib
  • 1
  • 1
  • 1
    List should work – Airwavezx Jul 19 '20 at 12:56
  • 1
    Does this answer your question? [What does List> mean in java generics?](https://stackoverflow.com/questions/1844770/what-does-list-mean-in-java-generics) – Harmandeep Singh Kalsi Jul 19 '20 at 12:57
  • Thank you, I know that List work fine, but the question is here Why I can't add this list1.add(1L); list1.add(“hello”); to list1 to the unbound wildcard ? – Labib Jul 20 '20 at 08:14
  • Did you mean [differences between invariance, covariance and contravariance](https://proandroiddev.com/understanding-generics-and-variance-in-kotlin-714c14564c47)? – Animesh Sahu Jul 20 '20 at 08:41
  • No , but thanks for helping me. – Labib Jul 21 '20 at 17:29
  • The question is here Why I can't add this list1.add(1L); list1.add(“hello”); to list1 to the unbound wildcard ? – Labib Jul 21 '20 at 17:30

0 Answers0