0

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.

2
  • 1
    I'm pretty sure there is still something going on with @JvmSuppressWildcards. Commented Apr 3, 2020 at 4:59
  • @EpicPandaForce Exactly, thank you! Annotations JvmSuppressWildcards and JvmWildcard. Commented Apr 3, 2020 at 5:57

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.