kotlin and Java call each other example detail

  • 2021-01-22 05:10:10
  • OfStack

preface

Interoperability is the ability to call interfaces of other programming languages in Kotlin. As long as they open their interfaces, Kotlin can call their member properties and member methods, which is unmatched by other programming languages. At the same time, the API interface in Kotlin can also be called during Java programming.

1. Call the ES11en method in kotlin

Kotlin and Java are two different languages, so there is a special syntax for calling each other. By default, setter and getter methods are used on the properties of objects in kotlin, so when you call Java in kotlin, you can directly click on the properties of setter and getter to get some operations on the properties. mAccount.setAccount (" mAccount.setAccount "); mAccount.setAccount (" mAccount.setAccount "); mAccount.setAccount (" mAccount.setAccount "); mAccount.setAccount (" mAccount.setAccount "); Or mAccount. getAccount (); Call it like this.

void methods and strings in Java are called 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 teenager "
mAccount.token = "0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6"
println(mAccount.account)
println(mAccount.token)
println(mAccount.date)

Log output:


.../com.sample.app I/System.out:  Qinchuan teenager 
.../com.sample.app I/System.out: 0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6
.../com.sample.app I/System.out: 2018-01-31 10:50:48

The Java array is called in kotlin

Java example:


public class Books {
 public List<String> getBooksList(){
  List<String> mBooks = new ArrayList<>();
  mBooks.add(" The snow ");
  mBooks.add(" The waking of insects ");
  mBooks.add(" The wind west gansu ");
  mBooks.add(" Sunvo cassock ");
  mBooks.add(" At home alone ");
  mBooks.add(" You can't 6 remember ");
  mBooks.add(" The Story of the Sahara ");
  mBooks.add(" Complete collection of poems by cangyang gyatso ");
  return mBooks;
 }
}

kotlin example:


val mBooksList = Books()
val mBooks = mBooksList.booksList
for (book in mBooks){
 println("$book")
}

Log output:


.../com.sample.app I/System.out:  The snow 
.../com.sample.app I/System.out:  The waking of insects 
.../com.sample.app I/System.out:  The wind west gansu 
.../com.sample.app I/System.out:  Sunvo cassock 
.../com.sample.app I/System.out:  At home alone 
.../com.sample.app I/System.out:  You can't 6 remember 
.../com.sample.app I/System.out:  The Story of the Sahara 
.../com.sample.app I/System.out:  Complete collection of poems by cangyang gyatso 

An Java static member is called 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

In Java, attributes of objects in kotlin are assigned

kotlin example:


class DataUtils {
  //  Basic data types 
  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 sample


val mAccount = Account()
mAccount.account = " Qinchuan teenager "
mAccount.token = "0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6"
println(mAccount.account)
println(mAccount.token)
println(mAccount.date)
0

Log output


val mAccount = Account()
mAccount.account = " Qinchuan teenager "
mAccount.token = "0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6"
println(mAccount.account)
println(mAccount.token)
println(mAccount.date)
1

Note: In kotlin the Char type is no longer a numeric type.

Function methods and arguments from kotlin are called in Java

kotlin sample


val mAccount = Account()
mAccount.account = " Qinchuan teenager "
mAccount.token = "0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6"
println(mAccount.account)
println(mAccount.token)
println(mAccount.date)
2

Java sample


DataTest mData = new DataTest();
// 
mData.doPrint();
//  call kotlin Method, and carries parameters 
mData.setPhone("176*****200");

Log output:


val mAccount = Account()
mAccount.account = " Qinchuan teenager "
mAccount.token = "0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6"
println(mAccount.account)
println(mAccount.token)
println(mAccount.date)
4

A static member in kotlin is called in Java

If all the members of a class are static, change from class to object instead of wrapping every method with companion object{}.

kotlin sample


val mAccount = Account()
mAccount.account = " Qinchuan teenager "
mAccount.token = "0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6"
println(mAccount.account)
println(mAccount.token)
println(mAccount.date)
5

Java sample


val mAccount = Account()
mAccount.account = " Qinchuan teenager "
mAccount.token = "0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6"
println(mAccount.account)
println(mAccount.token)
println(mAccount.date)
6

Log output:


val mAccount = Account()
mAccount.account = " Qinchuan teenager "
mAccount.token = "0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6"
println(mAccount.account)
println(mAccount.token)
println(mAccount.date)
7

If only a few members are static, you need to wrap the members with companion object {}.

kotlin sample


class KotlinUtils {
  //......
  companion object {
    fun name(): String {
      return " Qinchuan teenager "
    }
  }
}

Java sample


val mAccount = Account()
mAccount.account = " Qinchuan teenager "
mAccount.token = "0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6"
println(mAccount.account)
println(mAccount.token)
println(mAccount.date)
9

Log output:


.../? E/ The output :  Qinchuan teenager 

Note: The two static methods are not the same. The first method is written by the INSTANCE keyword, and the second method is written by the Companion keyword.

conclusion


Related articles: