JvmOverloads
Create a class for Kotlin
class Student(val name: String, val sex: Int = 1, val age: Int = 18)
Copy the code
The kotlin constructor has a default value for its parameters.
val student = Student("wuyue")
val student2 = Student("wuyue", age = 18)
Copy the code
But this feature will fail if you call it in Java.
Note that the following method call is an error, cannot be called, can only select three constructor methods.
What if I have to make Java callable? Add a note:
class Student @JvmOverloads constructor(val name: String, val sex: Int = 1, val age: Int = 18)
Copy the code
This is very important for Android programmers, for example, we need this annotation in the custom view, otherwise the runtime will not find the method error.
So just keep in mind that when a method in your Kotlin code uses default values, which is a kotlin language feature, you’d better add JvmOverloads to it when the method is called by Java code
JvmName
Let’s add an extension function to String: stringShelper.kt
package com.test
fun String.appendUserName():String{
return this+"wuyue"
}
Copy the code
How do you call it in the Java world?
StringsHelperKt.appendUserName("hello");
Copy the code
That makes sense, right, but many people are used to using xxxUtils in the Java world to handle similar situations. This is where the JvmName comes in
@file:JvmName("StringsHelperUtils")
package com.test
fun String.appendUserName():String{
return this+"wuyue"
}
Copy the code
So we call his method in the Java world
StringsHelperUtils.appendUserName("hello");
Copy the code
JvmMultifileClass
The comment on the web is that you can merge code from 2 KT files into a Java class file.
FunA.kt
@file:JvmName("Utils")
@file:JvmMultifileClass
package com.test
fun one(){
}
Copy the code
FunB.kt
@file:JvmName("Utils")
@file:JvmMultifileClass
package com.test
fun two(){
}
Copy the code
This gives you one and two methods for calling Utils in the Java world. Otherwise the compilation will report an error.
JvmField
Again, the Student example
class Student( val name: String, val sex: Int = 1, val age: Int = 18)
Copy the code
If you want to call properties in Java code you can only call them with get and set. But if you annotate it
class Student(@JvmField val name: String, val sex: Int = 1, val age: Int = 18)
Copy the code
It can be called directly in the Java world
student.name="hello";
Copy the code
Another important point is that in Kotlin, val is not meant to be a constant, but instead declares a variable with no set method, only a get method. So it gives you the illusion of constant. To actually define a constant in Kotlin, there are only two ways:
- Use const val in top-level or object
- Or use @jvmField val. (This is a constant, so you can’t override val’s get method.)
JvmStatic
class StaticTest{
companion object{
const val field1="111"
val field2="222"
@JvmField val field3="333"
fun callNonStatic(){
}
@JvmStatic
fun callStatic(){
}
}
}
Copy the code
So let’s look at how we call them in Java code and see what this annotation actually does, okay
StaticTest.callStatic();
StaticTest.Companion.callNonStatic();
String t1 = StaticTest.field1;
String t2=StaticTest.Companion.getField2();
String t3=StaticTest.field3;
Copy the code
JvmSynthetic
This annotation is not used very much, but many of the official Kotlin libraries use it and it’s clear that if you’re writing a function that you only want to call kotlin code and not Java code, you can just add this annotation to your function
Such as:
@file:JvmName("StringsHelperUtils")
package com.test
@JvmSynthetic
fun String.appendUserName():String{
return this+"wuyue"
}
Copy the code
So you can see that this is a function that you normally use with Kotlin whereas StringsHelperUtils in Java code doesn’t have this method