How to convert from Java to Kotlin code: start using Kotlin now (KAD 29)

Source: Internet
Author: User

Antonio Leiva

Time:Jul, 4,

Original link:https://antonioleiva.com/kotlin-from-java/

Kotlin one of the most magical features is its ability to Java is fully integrated. This means that although all of your application's code is written in Java , you can still Create a class in Kotlin from It is used in Java, and there is no problem.

Here are two benefits:

    • you can be in Java used in Projects Kotlin: In any project that has started, you can now start writing new code with Kotlin. It is then called from the Java code.

    • If you have Kotlin still haunted, can be in Java in this section : Many people ask me if there is a shortage of kotlin in some cases on Android. In theory, everything is up to the task, but in reality, there's no way to know (currently, no one has done "everything" on Android with Kotlin). The fact is, it doesn't matter if some operations can't be done in Kotlin, you can also go back to Java and implement it.

today we will look at how this compatibility works and how to Java uses Kotlin code.

Package-level functions

in kotlin java utils.kt

1 Fun logd (message:string) {2     LOG.D ("", Message)3}45funLogE (message:string) {6      log.e ("", Message)7 }

in the in Java, we can access them through the Utilskt class and use some static methods:

1 utilskt.logd ("Debug"); 2 utilskt.loge ("Error");

in the previous article, you've seen that I like extension functions. and in Java , how do they do that? If we have the following:

1 false ): View {2     returnthis, attachtoroot)3 }

Attention:

Although they may appear at some point, I have yet to explain this explicitly. The arguments to the function may have default values. This means that if we do not specifically specify, they will use the value specified at the time of declaration. This prevents us from using method overloads if we want to use them in Java.

This function is used to ViewGroup . It receives a layout and expands it in its parent view.

If we are going to What do you get when you use it in Java?

1 false);

as you can see, the object (receiver) to which this function is applied is added as a parameter to the function. Also, since We cannot use the default values in Java, it is mandatory to select the parameters.

if you want to The appropriate overloads are generated in Java, and you can use @JvmOverloads annotations for the function . This way, you do not need to Specify false in Java:

1 @JvmOverloads 2 false ): View {3     returnthis, attachtoroot)4 }

1 View v = utilskt.inflate (parent, R.layout.view_item);

If you want to When you specify a class name in Java, you can use annotations to modify it. in file utils.kt , add before the package :

1 @file: Jvmname ("Androidutils")

now The classes in Java will be named:

1 androidutils.logd ("Debug"); 2 androidutils.loge ("Error"); 3 false);

instances and Static fields

in the in Java , we use fields to store state. They can be instance fields, which means that each object has its own, or static (all instances of the class will share them).

if we try to Kotlin , then it will be the attribute and accompanying object. If we have such a class:

1 classapp:application () {2 3Val Apphelper =Apphelper ()4 5 Companion Object {6 lateinit var instance:app7     }8 9 override Fun OnCreate () {Ten         Super. OnCreate () OneInstance = This A     } -  -}

this in How does it work in Java? You can simply access the accompanying objects as static fields, as well as the properties that use getter and setter :

1 Apphelper helper = App.instance.getAppHelper ();

you will compile without problems. as Val, it only generates getter in Java . If it is var, we will also have a setter.

because it uses the lateinit comments, Access instance has been automatically worked, and it automatically exposes The fields Kotlin used to store the state. But suppose we create a constant:

1 Companion Object {2    lateinit var instance:app3     val CONSTANT = 4 }

you will see that you cannot access it directly. You must access through the Companion internal class:

1 KotlinTest.Companion.getCONSTANT ()

Who's better? To expose a static field in the same way in Java, you need a new comment:

1 @JvmField val CONSTANT = 27

you can now use Java code:

1 int c = app.constant;

If you have a function in the companion object, you can use the @JvmStatic Comment to convert it to a static method.

Conclusion

you see, by Java uses the Kotlin code very simply. I have shown some of the most typical examples here, and others can be implemented in very similar ways.

I hope if you have any questions, this will persuade you to start using the project Kotlin. If you want to think seriously, I suggest you read This group of Kotlin articles , you can learn more about Kotlin the information.

How to convert from Java to Kotlin code: start using Kotlin now (KAD 29)

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.