0 x1, principle

Tells the compiler to automatically generate multiple overloads of this method

Instead of writing your own overloaded methods, KT will automatically generate them for you, such as the following code:

@JvmOverloads 
fun search(name: String? = null, price: Float = 0.0 f, kind: Int = -1) {}
Copy the code

This is equivalent to declaring three overloaded methods in Java:

void search(String name)
void search(String name, float price)
void search(String name, float price, int kind)
Copy the code

But!!! Is the bottom layer really converted to these three methods? Not really! Decompile bytecode below:

A search$default() method is generated, which is called by other methods as arguments.

What is confusing is the number &1, &2, &4, and the number 4, 6, and 7.

Plus code for calling overloaded methods:

fun main(a) {
    val book = Book()
    book.search("Kotlin")
    book.search("Kotlin".66.6 f)
    book.search("kotlin", kind = 1)
    book.search(price = 100.0 f)}Copy the code

Decompile bytecode below:

SearchBook $default = searchBook$default = searchBook$default = searchBook$default = searchBook$default = searchBook$default

6-0110-0001 | 6 & 1-0000 = > the result is 0, do not reset the value of var1 2-0010 | 6 & 2-0010 = > the result is not zero, Reset var2 value 4-0100 | 6 & 4-0100 = > the result is not zero, reset var3 valueCopy the code

See here, quick thinking friends should see the clue, not urgent, try again 4:

4-1-0001 | 0100 4 & 4-0000 = > the result is 0, do not reset the value of var1 2-0010 | 4 & 2-0000 = > the result is 0, Don't reset var2 value 4-0100 | 4 & 4-0100 = > the result is not zero, reset var3 valueCopy the code

For those of you who haven’t got the square root of this, think about this: 6 = 0 + 2 + 4, 4 = 0 + 0 + 4

Each argument corresponds to a value → 2^(number of arguments -1). If this parameter is passed, it is not added.

Don’t believe it? Try adding another parameter:

@JvmOverloads
fun search(name: String? = null, price: Float = 0.0 f, kind: Int = -1, author: String? = null) {}Copy the code

Take a look at the decompilation effect:

It is not difficult to see:

14 is 0 + 2 + 4 + 8 12 is 0 + 0 + 4 + 8 10 is 0 + 2 + 0 + 8Copy the code

Use this value to determine whether a parameter is passed, without directly assigning the initial value, reducing the generation of unnecessary overloaded methods.

0x2. A Small detail

In real development, the most common scenario @jvmoverloads will be when customizingviewloads without writing these three constructors:

When you inherit a View, AS will remind you whether to add this @jvmoverloads:

Click after automatically add, very fragrant ~

When we reference our custom EditText in the layout file, we find that there is no way to get focus and type??

In fact, with the source code to know:

The default value is not 0, it should be r.attr. editTextStyle, so the solution is simple, just change 0 to this value:

This can happen with other controls too, so try to troubleshoot @jvmoverloads when implementing custom views

References:

  • Interesting Kotlin default parameter with JVMOverloads

  • Do not always trust @JvmOverloads