The fourth Kotlin development skills you need to know

Get into the habit of writing together! This is the 7th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

1. @JvmNameModify the method name

Just look at the example:

    @JvmName("testCopy")
    fun test(name: String, age: Int) {
    }
复制代码

Decompile directly into java code to see:

image.pngThe final generated method name is testCopy instead of test

2. @get:JvmName, @set:JvmNamemodify the attribute name

@get: JvmName("getSource")
@set: JvmName("setSource")
var mData: String = ""
复制代码

Decompile directly into java code to see:

image.png

For valproperties you can only use@get: JvmName

3. String is empty and used isNullOrEmptyto avoidTextUtils.isEmpty()

Let's compare these two ways of writing:

image.png

image.png

It can be seen that if you use isNullOrEmptyan empty String, name.lengthyou will not report an error when you call it, but if you use it, you TextUtils.isEmpty()will report an error. You need to force the declaration to name!!not be empty`

Why does the first one not report an error, look at isNullOrEmptythe source code:

@kotlin.internal.InlineOnly
public inline fun CharSequence?.isNullOrEmpty(): Boolean {
    contract {
        returns(false) implies (this@isNullOrEmpty != null)
    }

    return this == null || this.length == 0
}
复制代码

The core is isNullOrEmptythat there is one in the method body contract, this will help the compiler to tell whether the String is empty, so that when the compiler is called name.length, the compiler can infer that the String is not empty, so there is no need for the program to force the declaration of name!!non-empty.

4. Ignore case comparisonequals

A general case comparison is as follows:

val res = "aa".toLowerCase(Locale.ROOT) ==  "AA".toLowerCase(Locale.ROOT)
复制代码

Decompile into java code and see:

image.png

As you can see, "aa".toLowerCase(Locale.ROOT)assign to a local variable var6, "AA".toLowerCase(Locale.ROOT)assign to another local variable var7, and then compare.

This way of comparison will create two additional local String variables, so it is recommended to use equalssubstitution, where the second parameter can specify to ignore case comparison

val res = "aa".equals("AA", ignoreCase = true)
复制代码

Decompile:

image.png

As you can see, the implementation is very simple and does not create additional local variables

5. Operator overloading get,set

  • get
class Pro {
    operator fun get(content: String): String {
        return content.repeat(10)
    }
}
复制代码

Then it can be used like this:println(Pro()["blue"])

  • set
operator fun set(key: String, value: String) {
}
复制代码

setOperator overloading requires at least two parameters to be passed in, which are used as follows:Pro()["key"] = "value"

There are many usage scenarios for these two operators, such as the read and write encapsulation of SharedPreference in Android. For details, please refer to the article: 3. Combination of Delegate and SharedPreference

There are many other operator overloaded functions, such as pluscorresponding to "+", containscorresponding to "in", etc., which are commonly used in daily development, and you can use them flexibly according to specific scenarios.

Guess you like

Origin juejin.im/post/7084008873987670047