Three minute introduction to kotlin programming

Why use kotlin

Phase I of the project is ending, and there is finally time to toss. For more than a month, kotlin has insisted on the personal experience of development from the beginning to the present. Because of the great difference between syntax and Java, I wanted to give up at the beginning. If it was not for the use of the project, few people would try such a minority language, but after getting used to it, I will find how much time has been wasted writing useless java code over the years. Kotlin can greatly improve the development efficiency on the basis of Java compatibility. Kotlin has many features, but for development, rapid adaptation and learning are more important. Many times we start using it without understanding the reason, just as it is impossible for us to study the retrofit principle thoroughly before using it. As long as it is stable and mature enough and can effectively improve development efficiency, it is worth trying. I'm not talented. The main reason for using kotlin at present is that it has concise syntax, supports lambda expressions, powerful when syntax and doesn't need to write semicolon endings. These alone are enough to make me like kotlin. As for features such as null security, after all, there are too many null data types on the server, and I don't feel much about them. These features have been absorbed imperceptibly in practical applications, so that I can't adapt to several Java codes.

Modification Description

Suddenly angry and confused, the impact of Google IO can't be underestimated. It's better to modify the basic knowledge of the previous article and remove the expired links.

Kotlin basic grammar

First tell me a little trick to learn kotlin syntax. Write the code in Java, and then convert the code / convert java file to kotlin file into kotlin code, or copy the Java code to kotlin file. It will prompt that the conversion is also very convenient. It is easy to understand the differences. It is inevitable to get used to it at the beginning. I believe you will like kotlin

1) . definitions

Forget the way Java is written, otherwise it will be very uncomfortable. Kotlin has its own characteristics and should not be bound by Java thinking. In kotlin, constants are declared with Val instead of VaR, keywords are in front, types are separated by colons, or direct assignment can be omitted, with a question mark after the type? Indicates a nullable type (the default is null security). The constant Val delays loading by lazy {}, the default thread safety is off, thread safety lazy (lazythreadsafetymode. None) {}, and the variable var delays loading lateinit.

2) . conditions

If... Else is used normally, but switch is removed and replaced with a more powerful when. The when sub formula can be various expressions that return Boolean

3) . cycle

While and do... While is no different from Java, for has changed a lot and has several more variants

Universal colon

Colon in kotlin: it's never too much to call it omnipotent. It is required for the type declaration of constant variables, the return value of functions, and the inheritance of classes

In addition, there is a special need for it when using java classes. Kotlin will eventually be compiled into Java bytecode. It is inevitable to use Java classes. The kotlin syntax is as follows

Class name: class.java. Why not write it like this? Just remember

Who am I@

In addition to the colon, another important symbol @, there must be many places where inner classes and anonymous inner classes are used. In addition, lambda syntax is supported. Without it, who will tell you which of this and return refers to

Lazy method

1) . the getter / setter method of kotlin featured Java is automatically converted into an attribute, corresponding to the call of kotlin attribute

Such a Java class only needs to be called in kotlin

Conversely, the property of Kotlin automatically generates the getter/setter method of Java, which is easy to invoke in Java, and is also defined in Kotlin.

Sometimes our getter / setter method is not so simple, so we need to customize the getter / setter. Set up another line to set the get () / set (value) method to implement a single example commonly used in Java. This is just for demonstration. The single example has a simpler method implementation in kotlin. Just create an object at the package level

The custom getter / setter focuses on the field. Just as this in Java refers to the current class, field refers to the current parameter. Directly using the parameter name instance instead will not report an error, but a single example will have no effect

2) String templates have poor code readability for splicing strings in Java. String splicing in kotlin becomes very concise. You only need to add parameter name after $and {} for complex parameters

3) At the beginning, lambda thought that lambda was very advanced and couldn't understand it at all. In fact, it was very simple to omit the interface name, method name and parameter type and add - > instead. If you understand this, you won't introduce it.

new face

1) . delayed loading 2). Process control

Null judgment

Kotlin's null security design requires null judgment processing for parameters that can be declared null. There are two processing methods: one is to throw an empty exception like Java and add!! after the field!!, The other is to skip directly without processing, and add "yes" after the field?

matters needing attention

The internal class and parameters are public by default, while the private class in Java is final by default. If you want to be inherited, you need to declare it as open or abstract. Cancel the static keyword. The static methods and parameters are written uniformly in the internal module of the company object block, which is visible and the inner inner inner class

The above is only a summary of the syntax used frequently. You can basically read kotlin code when you learn it. If you encounter some problems, Google will check it quickly. As for intelligent conversion and type derivation, needless to say, you will like it naturally after use. Of course, this is only the basic syntax of kotlin, which is convenient for Java to get familiar with kotlin quickly. Please read the relevant materials for in-depth study.

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
分享
二维码
< <上一篇
下一篇>>