The opening
As mentioned in the previous article, if kotlin’s parameter is not? It defaults to not null. This is a little bit useful when kotlin is compiling. Let’s take Java ints and integers for example. Integer is a wrapper type for int, which is more wasteful than int. As a good coder, you’ll want to use int instead of Integer if the scope allows.
case
So let’s look at two approaches
fun myList(vararg inner: Int) {
println(inner.size)
}
fun myList2(vararg inner2: Int?) {
println(inner2.size)
}
Copy the code
The only difference between these two methods is do you have an Int after it? Number. Let’s look at the important parts of the decompiled code first
public final class VarargThinkKt { public static final void myList(@NotNull int... inner) { //..... } public static final void myList2(@NotNull Integer... inner2) { //..... }}Copy the code
Look carefully at the signature of the parameter
myList
isint... inner
myList2
isInteger ... inner2
You can think about itkotlin
Why does the compiler compile like this?
Actually, the root cause is?
, can be empty.
当kotlin
When the compiler thinks the parameter cannot be empty, it can use notpackaging
To accept parameters and make more rational use of resources.
Death peeled cocoon
Let’s see how it works by example, okay
fun myList(vararg inner: Int) { println(inner.size) } fun myList2(vararg inner2: Int?) Println (inner2.size)} fun main() {val intArrayOf = intArrayOf(1, 3, 4) null) myList(*intArrayOf) myList(1, 2, 3) myList2(*array) myList2(1, 2, 3) }Copy the code
The above code needs to be noted:
- statement
Int
An array of type,kotin
Two methods are provided, one isintArrayOf()
, one isarrayOf()
; intArrayOf
Stress isint
, similar to Javaint[]
, so the parameter cannot benull
arrayOf
Similar to JavaInteger[]
, so you can assign to itnull
kotlin
An array cannot be assigned directly to a morphable parametervararg
, you need to pass the keyword*
- Reflect on that when we create
array
If you don’t need tonull
Are you usingintArray
Better?
Touch type plate
There is intArrayOf, so there should be longArrayOf and there is a similar design under Java8 Stream, let’s look at this thing
@FunctionalInterface
public interface IntConsumer {
void accept(int value);
}
@FunctionalInterface
public interface IntFunction<R> {
R apply(int value);
}
Copy the code
In fact, the above ideas are the same, the code is optimal.
Kotlin null or empty
Error will not even compile
For example:
val grantResults: IntArray? = null println("${grantResults? .size} ${grantResults? .isempty ()}") outputs: null nullCopy the code
var s: String? = "" var str: String = s ? : "bb" println("str:$str") println("str:${s? .length ? : "bb"}") output: STR: STR :0Copy the code
var s: String? = null var str: String = s ? : "bb" println("str:$str") println("str:${s? .length ? : "bb"}") STR :bb STR :bbCopy the code
var s: String? = "" var str: String = if (s.isNullOrEmpty()) "bb" else s println("str:$str") println("str:${s? .length ? : "bb"}") output: STR :bb STR :0Copy the code
var s: String? = null
var str: String = if (s.isNullOrEmpty()) "bb" else s
println("str:$str")
println("str:${s?.length ?: "bb"}")
输出:
str:bb
str:bb
Copy the code