Use and simple example of Android kotlin

Use and simple example of Android kotlin

As a person who doesn't stay up late, I wake up and find that kotlin has become the official language of Android. In order to strike while the iron is hot, I decided to release the article originally scheduled for this Sunday's release three days in advance. I hope you can get to know kotlin in time.

I believe many developers, especially Android developers, have heard of kotlin more or less. Of course, it doesn't matter if they haven't heard of it or are unfamiliar with it. Because this article and the later content of the blog will involve a lot of knowledge sharing about kotlin.

More than a month before writing this article, Flipboard China's Android project confirmed that kotlin was officially used as the project development language, which means that the new code files will appear in kotlin code format, and the old java code will be continuously translated into kotlin code. During the period of using kotlin, I was shocked by its simplicity, efficiency, quickness and other characteristics. Therefore, it is necessary to write an article to talk about the characteristics of kotlin. I am very glad if I can achieve the effect of promoting kotlin.

Kotlin's "resume"

JetBrains, a well-known tool developer, determines that kotlin's gene must contain features such as practicality and efficiency. Let's take a look at the characteristics of kotlin. Of course, this is an important reason why I use kotlin.

The grammar is simple, not simple

Null pointer security

Null pointer (NullPointerException or NPE) is the most common crash in our java development programs. Because in Java, we have to write a lot of defensive code, such as this

The hollow pointer exception in kotlin is well solved.

1. Processing on type, i.e. adding?, after type?, That means that the variable or parameter and the return value can be null, otherwise it is not allowed to assign null to the variable parameter or return null

2. For a variable or parameter that may be null, you need to add?, before calling the object method or property?, Otherwise, the compilation cannot pass.

For example, the following code is an example of kotlin's implementation of null pointer security, and it is almost done in one line of code compared with Java implementation.

On the principle of null pointer security, you can refer to this article to study some methods of kotlin

Support method extension

Most of the time, the APIs provided by the framework are often atomic, and we need to combine them when calling, because some util classes will be generated. For a simple example, we want to display toast information more quickly, which we can do in Java.

However, the implementation of kotlin is surprising. We only need to rewrite the extension method. For example, this longtoast method is extended to all context objects. If we do not trace the source, we may not be able to distinguish whether it is provided by the framework or extended by ourselves.

Note: kotlin's method extension does not really modify the corresponding class file, but deals with the compiler and IDE. Makes it look like we've extended the method.

Lambda, higher-order functions, streams API, functional programming support

The so-called lambda expression is an anonymous function, which makes our code simpler. For example, the following code is the application of lambda.

The so-called higher-order function is

Take an example of accepting a function as an argument. In Android development, we often use shared preference to store data. If we forget to call apply or commit, data modification cannot be applied. Using the function of higher-order function in kotlin, we can better solve this problem

Of course, in the above example, we also use methods to extend this feature.

Kotlin supports streams API and method references, which makes functional programming more convenient. For example, the following code is that we combine jsup to grab the data of a proxy website. The code is simpler and faster to implement.

String template

Whether it is Java or Android development, we will use string splicing, such as log output and so on. In kotlin, string templates are supported. We can easily complete the composition of a string array

Note: for string splicing, please refer to this article Java details: String splicing

Good interaction with Java

Both kotlin and Java are JVM based programming languages. Kotlin and Java interact well, which can be said to be seamless. This is reflected in

Kotlin is widely used

Kotlin supports a wide range of Android application development. Many tools, such as kottknife (butterknife kotlin version), rxkotlin, anko, etc., and of course, many existing Java libraries can be used.

In addition, kotlin can also be compiled into JavaScript. Recently, kotlin wrote a piece of code to grab proxy, which is very fast to implement. It's even much faster than pure JavaScript.

About performance

Kotlin's execution efficiency is theoretically consistent with that of Java code. Sometimes kotlin may appear higher. For example, kotlin provides inline settings for methods, which can set some high-frequency methods for inline operations, reducing the overhead of entering and exiting the stack and saving the state at run time.

After reading this, do you want to try kotlin? Its concise syntax, collection of many features, efficient implementation and so on have been popular abroad. Foreign companies such as pintereset, square and Flipboard have begun to be applied to production.

About turning to kotlin

In fact, before I made the decision (kotlin had not been appointed at that time), I also considered whether choosing kotlin meant giving up Java. Calm down and think about it. In fact, that's not the case, because kotlin is too close to Java syntax and deals with Java related things all the time in kotlin, so this concern is not a problem.

Turning to kotlin for personal projects is usually not a difficult choice. After all, kotlin is such an excellent language. I believe many people are willing to try and use this language with half the effort.

The more difficult choice is how to make the team switch to kotlin. Personally, I think there are many reasons why the team is difficult to switch, such as learning cost, historical burden and so on. But in fact, the root cause is still the problem of thinking mode. Waiguoren likes to use tools to improve development efficiency because of the high labor cost. The way to improve the efficiency of domestic teams is usually to increase their members. Fortunately, the Flipboard US team has introduced kotlin since 2015 (perhaps earlier), so it is more convenient for the Chinese team to choose kotlin. Of course, what's more important is that the current team size is small, and the members unanimously recognize the advantages of kotlin.

As for the team switching to kotlin's method, it is generally feasible to implement it from top to bottom. This means that either the direct technical leader is more open-minded, or someone needs to constantly promote to influence the team.

To make a more realistic comparison, Java is like an ordinary train from my hometown Baoding to the west of Beijing, which takes nearly 2 hours or even longer, while kotlin is the high-speed railway that takes only 40 minutes to arrive. Usually people choose high-speed rail because it saves time and improves the experience. This time and experience correspond to the code in programming. I think it should be high efficiency, high readability and maintainability.

Now, with the support of Google, kotlin to Android is believed to be in full swing in the near future. Tampering with Python's famous saying "life is short, I use kotlin", such an efficient and practical language should be accepted by more and more teams and applied to development and production. Of course, I also hope to shine in the domestic environment.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>