Cycle and anomaly of kotlin basic learning

preface

Kotlin is not as powerful as expected, and it is not difficult. I prefer to regard him as a grammar candy. The so-called grammar candy is an auxiliary tool that can make the code easier to read. Tools can be mastered by reading the instructions and practicing them several times. They are all memory things. Practice makes perfect. If you have a solid Java foundation, look at kotlin's development documents, write a demo, and get started in minutes.

Speaking of kotlin improving the efficiency of writing code, let's talk about some of the most intuitive and simple examples:

1. Instead of going to findviewbyid or @ bindview, get the control ID and use it directly

2. There is no need to write a bunch of getter and setter methods for bean

3. Support the new features provided by Java 8, such as lambda expression, functional interface, stream API, etc

introduction

In kotlin's loop statement, while loop and do... While loop are consistent with those in Java, which will not be described in detail here, while for loop is very different from those in Java, and exception handling is also different.

Iterative number

The for loop in Java always initializes the value first, judges whether the value is within the limited range, and updates the value every time until it exceeds the range.

In kotlin, the for loop does not have such a usage, but in order to replace this common usage, kotlin uses the concept of interval.

The difference is essentially the interval between two values, an initial value and an end value, expressed by the.. operator, usually a number.

The above expression is equivalent to 0 < onetoten < = 10 in Java.

The interval in kotlin is closed, and the end value is always part of the interval.

So, for example, how to use the for loop to print numbers between 0 and 100?

Here, the initial value of I is 0 and the end value is 100. Equivalent to Java code.

At this time, there is a new demand to print numbers from 100 to 0 in reverse order and only even numbers.

Here we can see that downto and step are used. See the meaning of the name. Downto indicates the reverse order from 100 to 0, step indicates the interval, and the default step 1 is not written. Equivalent to Java code.

Iterative set

Whether in Java or kotlin, the most used scenario for loops is the iteration of collections.

First, let's look at iterating over a map collection in Java and printing out key value pairs.

In Java, first get the key set, traverse the key set and get the corresponding value.

So how to traverse in kotlin?

How about it? Is it much more convenient.

At the same time, there is another simple trick in the above code: you can use map [key] to get the value or map [key] = value to set the value without calling the get () put () method.

.. Syntax can create not only numeric intervals, but also character intervals val atof = 'a'..'f '

If you want to iterate over a list collection, do this in Java:

Next, use kotlin to implement:

Here we use a new method until, which is equivalent to 0.. ls.size-1. Similarly, we can also use the withindex method to achieve the same effect as the map collection traversal, and obtain the keys and values at the same time.

Kotlin uses in to check whether a value is in an interval or its inverse operation! in。 At the same time, in can also be used as a branch of when.

Exceptions in kotlin

Exception handling in kotlin is roughly the same as in Java.

At the same time, the throw structure and try... Catch structure in kotlin are expressions like the if statement in kotlin, which can be used as part of another expression.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

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