Preface
Interoperability means that in Kotlin, the interfaces of other programming languages can be called. As long as they open the interface, Kotlin can call its member properties and member methods, which is incomparable to other programming languages. At the same time, the API interface in Kotlin can also be called when programming Java.
1. Call Java methods in kotlin
Kotlin and Java are two different languages, so when calling each other, there will be some special syntax. The object properties in kotlin have setter and getter methods by default, so when calling Java in kotlin, you can directly obtain the setter and getter operations of the property by directly calling Java in kotlin. For example, the following Java object can also be called directly in kotlin by mAccount.setAccount("Qinchuan Young Adult"); or mAccount.getAccount();
Calling void methods and strings in Java in kotlin
Java example:
public class Account { private String account; private String token; public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getToken() { return token; } public void setToken(String token) { this.token = token; } public String getDate() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date()); }}kotlin example:
val mAccount = Account()mAccount.account = "Qinchuan young player"mAccount.token = "0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6"println(mAccount.account)println(mAccount.token)println(mAccount.date)
Log output:
.../com.sample.app I/System.out: Qinchuan young player.../com.sample.app I/System.out: 0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6.../com.sample.app I/System.out: 2018-01-31 10:50:48
Calling Java array in kotlin
Java example:
public class Books { public List<String> getBooksList(){ List<String> mBooks = new ArrayList<>(); mBooks.add("Snow Festival"); mBooks.add("Shooting"); mBooks.add("Wind rises from Longxi"); mBooks.add("Mountain and River Casso"); mBooks.add("Free and Alone"); mBooks.add("Six Records of Floating Life"); mBooks.add("Stories of the Sahara"); mBooks.add("Complete Collection of Poetry of Tsangyang Gyatso"); return mBooks; }}kotlin example:
val mBooksList = Books()val mBooks = mBooksList.booksListfor (book in mBooks){ println("$book")}Log output:
.../com.sample.app I/System.out: Snow Festival.../com.sample.app I/System.out: Shocking.../com.sample.app I/System.out: Wind swells in Longxi.../com.sample.app I/System.out: Mountains and rivers.../com.sample.app I/System.out: Walking freely.../com.sample.app I/System.out: Six Records of Floating Life.../com.sample.app I/System.out: The Story of the Sahara.../com.sample.app I/System.out: Complete Collection of Poetry of Tsangyang Gyatso
Calling Java static members in kotlin
Java example:
public class DateUtils { public static String getDate() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date()); }}kotlin example:
val mDate = DateUtils.getDate() println("$mDate")Log output:
.../com.sample.app I/System.out: 2018-01-31 10:50:48
2. Call the kotlin method in Java
Assign values to objects in kotlin in Java
kotlin example:
class DataUtils { // Basic data type var mByte: Byte? = null var mShort: Short? = null var mInt: Int? = null var mChar: Char? = null var mLong: Long? = null var mFloat: Float? = null var mDouble: Double? = null var mBoolean: Boolean? = null // Reference data type var mName: String? = null}Java examples
DataUtils mData = new DataUtils();mData.setMInt(1000000000);mData.setMChar('A');mData.setMLong(System.currentTimeMillis());mData.setMFloat(100.0F);mData.setMDouble(100.0);mData.setMBoolean(true);System.out.print("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ar()+"/n");System.out.print(mData.getMLong()+"/n");System.out.print(mData.getMFloat()+"/n");System.out.print(mData.getMDouble()+"/n");System.out.print(mData.getMBoolean()+"/n");System.out.print("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Log output
.../com.sample.app I/System.out: -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------.../com.sample.app I/System.out: Qin Chuan Xiaosheng
Note: In kotlin, the Char type is no longer a numeric type.
Calling function methods and parameters in kotlin in Java
kotlin example
class DataTest{ // fun method fun doPrint(){ println("function method doPrint()") } // fun method with parameters fun setPhone(phone: String) { println("$phone") }}Java examples
DataTest mData = new DataTest();// mData.doPrint();// Call the method in kotlin and carry the parameter mData.setPhone("176******200");Log output:
.../com.sample.app I/System.out: Function method doPrint() in kotlin.../com.sample.app I/System.out: 176******200
Calling static members in kotlin in Java
If all members of a class are static members, changing class to object will not require each method to be wrapped with companion object{}.
kotlin example
object KotlinUtils { fun getName(): String { return "Qinchuan Young Player" } fun add(number1: Double, number2: Double): Double { return number1 + number2 }}Java examples
String mName = KotlinUtils.INSTANCE.getName();Log.e("Output", mName);double mNumber = KotlinUtils.INSTANCE.add(2.0, 3.0);Log.e("Output", Double.toString(mNumber));Log output:
.../? E/Output: Qinchuan young player.../? E/Output: 5.0
If only individual members are static members, members need to be wrapped with companion object {}.
kotlin example
class KotlinUtils { //......companion object { fun name(): String { return "Qinchuan Young Man" } }}Java examples
String mName = KotlinUtils.Companion.name();Log.e("Output", mName);Log output:
.../? E/Output: Qinchuan young player
Note: The two static writing methods are different. The first writing method is through the INSTANCE keyword, and the second writing method is through the Companion keyword.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.