Pros and cons of Kotlin

Starting with Android 7.0, the API used by Google switched from Oracle JDK to open JDK, which was a tough decision for Google. For developers, they are very excited, which means that the long-term lawsuit may be over, and Android system development can return to a normal track, continuing to bring surprises to Android users around the world.

What are the advantages of the Kotlin language?

The Kotlin language has been hailed as the Swift of the Android world.

In short, it can be summed up like this:

  • Fully compatible with Java
  • Null safe
  • Support for lambda expressions (better than Java8)
  • support extension
  • Experience a consistent development toolchain

What does using Kotlin mean for Android developers?

Using Kotlin to develop, for Android development, there are the following beneficial effects:

  • Fewer null pointer exceptions
  • less code
  • faster development speed
  • A more consistent development experience

What is Kotlin

The Kotlin language was developed by JetBrains , and its original intention was to quickly generate JavaScript code, and now Kotlin has supported Android development and provides a series of Android development plug-ins. Due to the natural advantages of JetBrains (Android Studio is made by adding some plug-ins to the IDE developed by JetBrains), Android Studio's support for Kotlin language is also in place.

Take a look at what the Kotlin language brings us.

Null Safe

I once mentioned this problem in the article Looking at Java8 from Swift . The null pointer exception is indeed a problem that has plagued Java programmers for many years. The Swift language subtly solves this problem. Kotlin adopts the same solution, but the syntax is not very form. Same.
Let's make a simple comparison with the Java language:

Java

Room room = ...;
if(null != room && null != room.window) {
     room.window.open();
}

Kotlin

val room: Room? = ...
room?.window?.open()

From common sense, we know that a room may or may not have a window. If there is no window, a null pointer exception will naturally occur when the open method is called. The way the Java language handles it is to do a null value judgment before calling the method, while the Kotlin language uses ? operator to control, that is, if ? If the previous object is empty, it will directly return a null value, and the following statement will not be executed. It has a professional name: Optional Value (optional value)

Compared with the Java language, the processing method of the Kotlin language is much simpler, and more importantly: Mom no longer has to worry about the null pointer exception in my program -_-

Careful readers should be able to find that the Kotlin language also removes the semicolon, which is consistent with the Swift language.

Regarding the specific usage of Optional Value, I will dedicate a chapter to explain it to you later. Please look forward to my Kotlin language series tutorials later.

Function

In the Kotlin language, classes are finally no longer first-class citizens. The Kotlin language begins to support procedural programming. The Kotlin language can declare global functions, inline functions, etc. It also supports functions nesting, using functions as method parameters, and other operations. For some simple operations, creating a new class to deal with is indeed sometimes a headache, and the Kotlin language finally lets us get rid of this embarrassing status quo.

Let's take a look at the simple way of writing function declarations, and make a simple comparison with the Java language

Java

public Sting sayHello(String name) {
   return "Hello, " + name; } 

Kotlin

fun String sayHello(name: String?): String {
   return "Hello, $name" } 

The writing method of functions is quite different. Kotlin syntax is similar to the writing method of Swift language. Each function must be declared with the fun keyword. The parameter type is after the parameter name. Process.

Similarly, I will explain Kotlin functions specifically in a later article

Lambdas

Closures should be a feature that Java programmers are looking forward to very much, and fortunately, Java 8 has begun to support this feature. The two are very similar, let's make a simple comparison with Java8's closure

Java8

interface Sum {
    int add(int x,int y); } Sum sum = (x,y) -> x + y 

Kotlin

val sum: (Int,Int) -> Int = { x,y -> x + y }

It can be seen that the syntax of the two is very similar, and they also support type deduction and abbreviation; the difference is that due to the completely object-oriented characteristics of the Java language, the closure must correspond to the interface one-to-one, while the Kotlin language naturally supports functional programming, You can use it directly in the parameters without declaring the corresponding interface.

Extension

This is one of my favorite features, the Kotlin language supports extending existing classes. Java programmers should be relatively unfamiliar with this feature, which is also one of the features of the Swift language. The so-called extension is to add new methods, properties and other operations to the existing class without using inheritance. One principle should be remembered: extension is better than inheritance
and this feature is not supported by Java language.

Kotlin

fun MutableList<Int>.swap(index1: Int, index2: Int) {
      val tmp = this[index1] // 'this' corresponds to the list
      this[index1] = this[index2]
      this[index2] = tmp } 

Data Class

In the development process, we often have to constantly write some Model classes and use development tools to generate set/get methods. Data Class is born to simplify this operation. The data class will automatically generate set/get methods instead of explicitly generating set/get methods. See the following simple usage:

Kotlin

data class Person(var name: String?,var age: Int?) 

Just use such a line of code to complete the creation of the Model class, and Kotlin will automatically generate set/get methods for us, which greatly simplifies the writing of the Model class. However, Java programmers don't have to be too envious, many Java frameworks can also achieve this, such as the Auto Value framework launched by Google, etc.

Operator Overloading

This feature also exists in the Swift language, and also exists in the C++ language. This feature is a double-edged sword, using operator overloading simplifies usage, but also loses some code readability. So, please think carefully when using operator overloading.

The features of the Kotlin language will not be finished for a while. Let's make a simple comparison between some of the more important features and the Java language:

language features Kotlin Java
Null Safe support Java8 support
global function support not support
Lambdas support Java8 support
Extension support not support
Data Class support Not supported (requires framework dependency)
Operator Overloading support not support

Java language is fully interoperable

You might worry about what to do with the previous Java code after using the Kotlin language. This worry is completely redundant, JetBrains guarantees that Kotlin and Java languages ​​can fully call each other, and Kotlin will eventually be compiled into Java bytecode.

Kotlin language advantages

According to statistics from some netizens, using Kotlin language can reduce code writing by about 30%, and at the same time, there is no need to worry about null pointer exceptions. Due to the natural genes of JetBrains, Kotlin can be better maintained and upgraded. In the end, it's a good counterattack for Oracle's frequent lawsuits against Google. After the release of the Kotlin language, many foreign developers have launched a large number of useful Kotlin frameworks, and you don't need to repeatedly create wheels. At the same time, the Kotlin language can be seamlessly connected with the Java language, so what are you waiting for?

suggestion

Regarding the Kotlin language, someone asked on Zhihu how to evaluate the Kotlin language? , Most developers have a positive attitude towards the Kotlin language. I also highly respect the Kotlin language. If you are familiar with scripting languages ​​such as Python and Ruby, or familiar with the Swift language, the Kotlin language will be your best choice. And if you are tired of the redundant and complicated writing of the Java language, the Kotlin language is also worth a try. I believe you will fall in love with this language like me.



Kotlin's pitfalls and how to avoid -- pitfalls

http://www.infoq.com/cn/news/2017/10/Kotlin-Trap-avoidance#


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325060629&siteId=291194637