Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

👉 About the author

As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!

Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)

Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!

👉 premise

The current environment

2020.3.1 Patch 2 version was last downloaded on October 8, 2021

👉 Practice

😜 Null check

Null security is one of Kotlin’s advertised features, but it’s not insensitive. It’s all about enforcing code specifications so that you can write code with good Null judgment. Java can also do this, but because it is subjective, anyone can have the possibility to drop.

So Kotlin came to do this for us, and if you don’t comply, you’re not going to compile the project directly.

So variable declarations cannot be null and require special handling, much like TypeScript.

Add [!!] after field (double exclamation mark) throws a null exception, plus? The default variable cannot be null. An error will be reported.

// Type after? It can be null
var myAge: String? = "666"
myAge = null
// Throw a null pointer exception if the variable is nullval ageZero = myAge!! .toInt()// if myAge is empty, ageOne is emptyval ageOne = myAge? .toInt() Log.e("TAG"."Sesame seed output 1:" + ageOne)
// Return -1 when myAge is emptyval ageTwo = myAge? .toInt() ? : -1
Log.e("TAG"."Sesame seed output 2:" + ageTwo)
Copy the code

😜 Type Check

Java has the instanceof keyword to check whether it is a type. Kotlin also has the instanceof keyword.

// Type after? It can be null
var myAge: String? = "666"
if (myAge is String) {
    // Determine whether the variable myAge is a string
}
Copy the code

Kotlin has a [Any] type, which, combined with [is], can be converted automatically, as follows

// Type after? It can be null
var myAge: Any? = "666"
if (myAge is String) {
    // Determine whether the variable myAge is a string
    Log.e("TAG".${myage.length}")}Copy the code

😜 interval

This is a new word, just as the name implies that it is a mathematical interval, such as the number from 1 to 10. Use [..] Implementation, two points not three, not to be confused with Java mutable arguments.

I haven’t thought of application scenarios yet

for (i in 1.6.)
  Log.e("TAG"."Sesame seed output 3: $I") / / output "123456"
for (i in 1.6. step 2)
  Log.e("TAG"."Sesame seed output 4: $I")// Set the step size, the step size and the interval are different.
// If there is a sequence, there is a flashback
for (i in 6 downTo 1)
  Log.e("TAG".Sesame seed output 5: $I) / / output "654321"
Copy the code

👉 other

📢 author: Kom and Kom in Kom

📢 reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)

📢 welcome to like 👍 collect 🌟 message 📝