preface

Kotlin is officially recognized by Google as a level 1 programming language for Android development. Today, I’m going to focus on some of Kotlin’s useful grammatical sweets, including:

  • Range: in, downTo, step, until
  • Type checking & conversion: IS, Intelligent conversion, AS
  • Equals (), ==, ===
  • Air safety


1. Scope of use

Indicates the range, including in, downTo, step, and until

/** * 1. [1,5] if (I in 1.. 5) {println(" I within 1-5 ")} He is not in school. If (I! in 1.. 1) {println(" I am not inside 1-5 ")} /** * 2. For (I in 1 until 5) {println(I)} /** * 3. **/ for (I in 1) downTo 1) {println(I)} /** * 4. Step * For (I in 1 downTo 5 step 2) println(I) // Set the step size to 2 and output 5, 3, 1 in reverse order for (I in 1 downTo 5 step 2) println(I)Copy the code

2. Type checking & conversion

Includes IS, Intelligent conversion, and AS

/** * 1. Is used to check whether an object is of the specified type **/ If (a is String) {println("a is String ")} if (a! Is Int) {println("a is not an Int ")} /** * 2. Note: Kotlin does not have to use explicit type conversion operations because the compiler tracks is checks for immutable values as well as explicit conversions and automatically inserts (safe) conversions when needed **/ var a: Any = "a" if (a is String) {println("a is String ") println(a.length) A is automatically converted to String if (a! Is String) {print (a. ength)} / / in && and | | right can also be intelligent transformation: / / ` && ` a automatically converted to the String on the right side of the if (a is a String && a. ength > 0) / / ` | | ` a automatically converted to the String on the right side of the if (a is a String | | a. ength > 0) When (a){is String -> a.length is Int -> a + 1} // Note: Smart conversions cannot be used when the compiler cannot guarantee that the variable will not change between checking and using. // 1. Val local variables -- always, except for local delegate attributes; // 2. val attribute -- If the attribute is private or internal, or the check is performed in the same module where the attribute is declared. Smart conversions do not apply to open properties or properties with custom getters; // 3. var local variable -- if the variable is not modified between check and use, is not captured in the lambda that will modify it, and is not a local delegate attribute; // 4. Var attribute -- never possible (because this variable can be changed by other code at any time) /** * 3. Cast: as **/ var any: any = "ABC" var STR: String = any as String Int = 123 var STR: String = String ClassCastException /** * 4. Nullable conversion operator: as? * Nulls cannot be converted to String because the type is not nullable. **/ var STR = null var str2 = STR as String // Throw TypeCastException // Use safe conversion operator as? You can return NULL if the conversion fails, avoiding throwing an exception. var str = null var str2 = str as? String println(str2) // The output is nullCopy the code

3. Equality judgment

In Kotlin, there are two kinds of equality judgment: structure equality and reference equality.

/** * 1. Equals () or == * Var a = "1" var b = "1" if (a.equals(b)) {println("a and b are equal "); Var a = 1 var b = 1 if (a == b) {println("a = b ")} /** * 2. Data class User(var name: String, var age: Int var a = User("Czh", 22) var b = User("Czh", If (c == d) {println("a and b are equal ")} else {println(" A and b are not equal ")} If (c === d) {println("a and b references are equal ")} else {println(" A and B references are not equal ")Copy the code

4. Air safety

  • NullPointerException is quite common in Java
  • Kotlin has the advantage of avoiding null-pointer exceptions when executing code as much as possible
In Kotlin, there are two situations that are most likely to cause a NullPointerException **/ / case 1: explicitly calling throw NullPointerException() // case 2: using!! Operator // description:!! The operator converts any value to a non-null type and throws an exception if it is null var a = null a!! / / throw KotlinNullPointerException / / case 3: data types cannot be null / / in Kotlin, distinguish between a reference type system can accommodate null (null reference) and does not fit (not null reference) / / such as: // To allow nullability, declare a variable as a nullable String: place a question mark after the String type. For String, it is written: String? var b: String? = "b" b = null /** * 2. .length // means: if b is not null, then b.length is called. .b? .c? .d // If a is not null, the whole expression will return NULL. // If you only perform an operation on non-null values, you can use a? .b? .let { println(it) }Copy the code

That concludes the introductory syntax for Kotlin.


5. To summarize

  • This article introduces some of Kotlin’s useful syntactic sugar
  • In the upcoming articles, I will continue to explain Kotlin, including its usage and syntax. If you are interested, please follow me on my blog: Carson_ho’s Android blog

Please give the top/comment a thumbs up! Because your approval/encouragement is my biggest motivation to write!