CtrlAltDelete CtrlAltDelete - 1 month ago 16
Java Question

Not understanding method types

Teaching myself Java and I came across a confusing part about method types/return types?

For example what does the

<U>
right after the
public
represent in the method below? How can it return a type of
<U>
and
List<U>
?

public <U> IList<U> map(IFunc<T, U> f) {
return new ConsList<U>(f.apply(this.first), this.rest.map(f));
}

Answer

It is called as a Type Paremeter. It represents a generic class that is representing within that map method. What I meant by generic class is, any class you can come up with. It can be MyHouse.java, MyCar.java, String.java, etc

IList<U> is, it's a IList that contains U type objects.

Comments