Why should I Switch to Kotlin? This paper address: https://www.oschina.net/news/84980/why-i-turn-to-kotlin. For Kotlin, I had resistance at the beginning, thinking that the language was a bit like Js, so I judged the language to be bad subjectively. Later, when I came to my new company, the boss demanded that all new code should use Kotlin, so I gradually fell in love with the language. A lot of people ask before they learn, is Kotlin worth learning? Before we rush to answer that question, let’s take a look at what makes Kotlin different from Java.
I’m sure many developers, especially Android developers, have heard of Kotlin at some point, but it doesn’t matter if they haven’t or aren’t familiar with it. This post and the rest of the blog will have a lot to share about Kotlin.
A little over a month before writing this article, Flipboard’s Android project in China officially adopted Kotlin as its development language, which means that new code files will appear in Kotlin code format. And at the same time old Java code will be translated into Kotlin code. During the period of using Kotlin, I was shocked by its concise, efficient and fast features, so it is necessary to write an article to talk about the characteristics of Kotlin, and I would be glad if I can achieve the effect of promoting Kotlin.
From well-known tool developer JetBrains, Kotlin’s DNA is bound to be functional and efficient. Let’s take a look at the characteristics of Kotlin, which is why I switched to Kotlin.
First, the syntax is simple and not wordy
1. Kotlin supports type inference without Java’s verbose. In addition, var is used to represent variables and val is used to represent constants. 3, the method is also very simple. Even function is shortened to fun, adding a bit of pun. 5, Kotlin every sentence does not need to add a semicolon (;).
Second, null Pointers are safe
Null Pointers (NullPointerException or NPE) are the most common crashes we see in Java development programs. Because in Java we have to write a lot of defensive code like this:
In Kotlin the hollow pointer exception is well resolved.
- Processing on a type, that is, following the type with? , that is, the variable or parameter and its return value can be NULL. Otherwise, assigning or returning NULL to a variable parameter is not allowed
- For a variable or parameter that may be null, before calling an object method or property, add? Otherwise, the compilation fails.
The following code is an example of Kotlin’s implementation of null-pointer safety, and it’s a one-line implementation compared to a Java implementation.
Third, support method extension
A lot of times, apis provided by the Framework tend to compare atoms and require us to combine them when called, because it generates Util classes. A simple example is that we want to display Toast information more quickly, and in Java we can do that.
But Kotlin’s implementation is amazing. We just need to rewrite the extension method. For example, the longToast method extends to all Context objects.
Note: Kotlin’s method extensions do not actually modify the corresponding class files, but rather deal with them on the compiler and IDE side. Makes it look like we’re extending the method.
4. About the Kotlin-Android-Extensions plugin
Kotlin not only has a good handle on null types, but also has many other advantages. The kotlin-Android-Extensions extension allows you to replace findViewById with the Android-based findViewById. Fragment and View will find the View object directly by the ID in the XML, no longer need to initialize the View object with findViewById repeated code. Although Android provides a DataBinding mechanism for developers to initialize View objects, DataBinding often generates errors during APK packaging, which is less of an issue than Kotlin, and is much simpler to use, requiring almost no initialization in the code. In addition, open source controls such as ButterKnife can omit the findViewById operation, though
Lambda, high-order functions, Streams API, functional programming support
Lambda expressions are anonymous functions, which makes our code much simpler. For example, the following code is an application of lambda. Java listens to the View callback as follows:
mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
.
}
});
Copy the code
mView.setOnClickListener {
.
}
Copy the code
Higher-order functions can take functions as arguments or return functions as results
Take an example that accepts a function as an argument. In Android development, we often use SharedPreference to store data, and data changes cannot be applied if you forget to call Apply or COMMIT. Using Kotlin’s higher-order functions, we can solve this problem better:
Of course, in the above example we also use methods to extend this feature.
Kotlin supports Streams APIS and method references to make functional programming easier.
al currentFilter =
mFilterList.filter { it is SearchResultFilter }
.map { it as SearchResultFilter }
.find { it.id == "1" }
Copy the code
The above shows using the functional API in Kotlin to find the element in the collection whose object type is SearchResultFilter and whose ID is 1. If you look at this code, you would definitely use a for loop if you were using Java, but using functions like Filter and map in Kotlin makes it easier to work with collections in some cases, and also makes your code more readable.
Let’s briefly talk about the functions of filter and map. Filter can help us filter out elements that meet the conditions, but filter does not change these elements, while Map can change these elements. Map can transform the original set into a new set according to a given operation.
Other Kotlin type functions include find, groupBy, flatMap, and Flatten. I don’t want to introduce them here. In addition, this function works better with RxJava, and you’ll find that writing elegant code is not as difficult as you might think.
String template
In both Java and Android development, we use string concatenation for logging and so on. In Kotlin, string templates are supported, and we can easily compose an array of strings:
Seven, good interaction with Java
Both Kotlin and Java are JVM-based programming languages. The interaction between Kotlin and Java is seamless. This is manifested in:
- Kotlin is free to reference Java code and vice versa.
- Kotlin can use all existing Java frameworks and libraries
- Java files can be easily converted to Kotlin with the help of IntelliJ plug-ins