Here are my Kotlin (Version 1.3.61) + Java(1.8.0_201) code that failed to compile:
Maven:
<dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>jsr305</artifactId> <version>2.0.1</version> </dependency> Test.Kt:
fun main(args: Array<String>) { Point().setX(1) } Point.java:
import javax.annotation.Nonnull; public class Point { public void setX(int x) { } public void setX(@Nonnull Integer x) { } } Failed to compile due to the following error:
Error:(2, 13) Kotlin: Overload resolution ambiguity: public open fun setX(@Nonnull x: Int): Unit defined in Point public open fun setX(x: Int): Unit defined in Point If I removed the @NonNull annotation in the second setX function, then this demo could compile. I thought Java annotation was only extra metadata and would not affect the code itself, so I had no idea why the ambiguity arose here.
Also, I run javap -s Point.class and found out that they had same output:
public void setX(java.lang.Integer); descriptor: (Ljava/lang/Integer;)V Can someone help to explain what is going on here? Thanks in advance.