2

I am reading an wildcard example in wildcard capture and helper methods in The Java tutorial. I don't understand why the compiler produces a capture error when compiled:

import java.util.List;

public class WildcardError{

    void foo(List<?> i){
        i.set(0, i.get(0));
    }
}

Why does the compiler processes the iinput as being of type object?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Jie
  • 21
  • 2
  • 2
    What input? What indication do you have that it handles the type `Object` here? What error does the compiler give you? – Sotirios Delimanolis Aug 18 '14 at 21:17
  • @SotiriosDelimanolis the List is some `List` and `i.get(0)` does return an `E` and the question is: why does the compiler complain about incompatible types when calling `i.set(int,E)`? – assylias Aug 18 '14 at 21:19
  • @assylias Right, for i.set(0, i.get(0)), the compiler required: int, CAP#1 but it found: int, Object – Jie Aug 18 '14 at 21:22
  • @Sotirios Delimanolis The tutorial says that the compiler processes the i input parameter as being of type object, but I don't know why. – Jie Aug 18 '14 at 21:26

0 Answers0