1

While learning java generics, I came across the wildcard type (?). In the tutorial they gave this example to illustrate its use case.

import java.util.List;

class MyList<T> {
    
    public List<T> list;
    
    public MyList(List<T> list) {
        this.list = list;
    }
    
    public boolean isLonger(MyList<?> other) {
        return list.size() > other.list.size()
    }
}

But I can define isLonger method as

public <U> boolean isLonger(MyList<U> other)

and its works fine.

So I am curious to know whether wildcard type has its own unavoidable use case, or it is just a syntactic sugar to the above line of code.

Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35

0 Answers0