Reputation: 2534
Obtained analogues syntax constructs for generic methods in Java and Kotlin:
public <T> void method() {}
fun <T> method() {}
public <T> T method(T... p) { return p[0]; }
fun <T> method(vararg p: T): T? { return p[0] }
public <T extends ClassA> T method(T[] p) { ... }
fun <T : ClassA> method(p: Array<T>): T { ... }
public <T extends ClassA & InterfaceA> T method(T[] p) {}
fun <T : ClassA> method(p: Array<T>): T where T : InterfaceA {}
public void method(Collection<? extends ClassA> collection) {}
fun method(collection: Collection<out ClassA>) {}
public void method(Collection<? super ClassA> collection) {}
fun method(collection: Collection<in ClassA>) {}
public void method(Collection<?> collection) {}
fun method(collection: Collection<*>) {}
Are there any other additional differences (syntactically or conceptual) for generalized methods in Java and Kotlin? Or innovations for generic methods in Kotlin? Thanks for any answer.
Upvotes: 0
Views: 263
Reputation: 61
In generic code, ‘?’ represents an unknown type. It is known as the wildcard. There are several uses of a wildcard, including as the type of a field, local variable, or parameter. While Java’s type system offers wildcard types, Kotlin doesn’t. However, it has two different things; declaration-site variance and type projections as an alternative to wildcard types.
taken from Kotlin vs Java: Most Important Differences That You Must Know
Upvotes: 2