Examples of mutual calls between kotlin and Java, and examples of kotlinjava

Source: Internet
Author: User

Examples of mutual calls between kotlin and Java, and examples of kotlinjava

Preface

Interoperability means that Kotlin can call interfaces of other programming languages. As long as they open interfaces, Kotlin can call their member attributes and member methods, which are incomparable to other programming languages. At the same time, you can call the API interface in Kotlin during Java programming.

1. Call the Java method in kotlin

Kotlin and Java are two different languages, so there are some special syntaxes when calling each other. By default, object attributes in kotlin carry the setter and getter methods. Therefore, when calling Java in kotlin, you can obtain the setter and getter operations of the attributes by using the direct variable name attribute. The following Java object can also be called directly by mAccount. setAccount ("Qinchuan Xiaozhi") or mAccount. getAccount (); In kotlin.

Call the void method and string 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()); }}

Example of kotlin:

Val mAccount = Account () mAccount. account = "" mAccount. token = "inherit" println (mAccount. account) println (mAccount. token) println (mAccount. date)

Log output:

... /Com. sample. app I/System. out: Qinchuan Xiaozhi... /com. sample. app I/System. out: 0xbE803E33c0BBd4B672B97158cE21f80C0B6f3Aa6... /com. sample. app I/System. out: 10:50:48

Call Java array in kotlin

Java example:

Public class Books {public List <String> getBooksList () {List <String> mBooks = new ArrayList <> (); mBooks. add ("Snow Festival"); mBooks. add ("stunned"); mBooks. add ("Fengqi Longxi"); mBooks. add ("mountains and rivers"); mBooks. add ("independent"); mBooks. add ("register six"); mBooks. add ("sub-Saharan Story"); mBooks. add ("cangyang jiacuo Poetry Collection"); return mBooks ;}}

Example of kotlin:

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: stunned... /com. sample. app I/System. out: wind up in the western region... /com. sample. app I/System. out: mountains and rivers... /com. sample. app I/System. out: independent... /com. sample. app I/System. out: the 6th note of Fusheng... /com. sample. app I/System. out: The story of Sahara... /com. sample. app I/System. out: a complete set of Poems delivered by cangyang jiacuo

Call 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()); }}

Example of kotlin:

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 attributes of objects in kotlin Java

Example of kotlin:

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 example

DataUtils mData = new DataUtils (); mData. setMInt (100000000); mData. setMChar ('A'); mData. setMLong (System. currentTimeMillis (); mData. setMFloat (10010000f); mData. set mdouble (100.0); mData. setMBoolean (true); System. out. print ("---------- basic data type ---------- \ n"); System. out. print (mData. getMInt () + "\ n"); System. out. print (mData. getMChar () + "\ 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 ("---------- reference data type ---------- \ n"); mData. setMName ("Qinchuan Xiaohui"); System. out. print (mData. getMName () + "\ n ");

Log output

... /Com. sample. app I/System. out: ---------- basic data type ----------... /com. sample. app I/System. out: 100000000... /com. sample. app I/System. out:... /com. sample. app I/System. out: 1517379001995... /com. sample. app I/System. out: 100. 0... /com. sample. app I/System. out: 100. 0... /com. sample. app I/System. out: true... /com. sample. app I/System. out: ---------- reference data type ----------... /com. sample. app I/System. out: Qinchuan Xiaohui

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

Call the function method and the method with parameters in kotlin in Java

Kotlin example

Class DataTest {// fun method fun doPrint () {println ("Function Method doPrint ()" In kotlin)} // fun setPhone (phone: String) with Parameters) {println ("$ phone ")}}

Java example

DataTest mData = new DataTest (); // mData. doPrint (); // call the method in kotlin and carry the mData parameter. setPhone ("176 ****** 200 ");

Log output:

... /Com. sample. app I/System. out: Function Method doPrint () in kotlin ()... /com. sample. app I/System. out: 176 ***** 200

Call static members in kotlin in Java

If all the members of a class are static members, changing the class to object does not require every method to be wrapped in companion object.

Kotlin example

Object KotlinUtils {fun getName (): String {return "Qinchuan xiao"} fun add (number1: Double, number2: Double): Double {return number1 + number2 }}

Java example

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 Xiaojiang .../? E/output: 5.0

If only some Members are static members, you need to wrap them with companion object.

Kotlin example

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

Java example

String mName = KotlinUtils. Companion. name (); Log. e ("output", mName );

Log output:

.../? E/output: Qinchuan Xiaohui

Note:The two static writing methods call different methods. The first method is through the INSTANCE keyword, and the second method is through the Companion keyword.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.