SoFunction
Updated on 2025-03-06

Complete explanation of the default value of Kotlin function

Function default value

As we know, Java language does not support the use of default values ​​for parameters. Some people say that this is because there is an ambiguity problem if "default parameters" and "method overloading" are supported at the same time. I don't know the specific reason. But for me personally, it is really a pity for me to not support this feature in Java. Although using method overloading can indirectly implement the same function as the default parameter feature, this means you have to write more code...

Brief introduction

When defining Kotlin functions, it supports specifying default values ​​for parameters, which effectively reduces the number of overloaded functions defined in Java before.

A brief comparison is as follows:

1. Java function definition. If the lastName in the sayHelloTo function can use the default value, you need to define the following two functions.

public class Person {
 public void sayHelloTo(String firstName,String lastName){
  ("Hi,"+firstName+" "+lastName);
 }
 public void sayHelloTo(String firstName){
  sayHelloTo(firstName,"noLastName");
 }
}

2. Kotlin is defined as follows, only one function is needed

class Person {
 fun sayHelloTo(firstName: String, lastName: String = "noLastName") {
  println("Hi,$firstName $lastName")
 }
}

Detailed rules for default value of Kotlin function

1. You can use the calling method of specifying all parameter names and not specifying parameter names. It is recommended to use the calling method of specifying parameter names (some function parameter names cannot be specified, and others are not specified)

Correct example:

sayHelloTo("Jerry","Aha")
sayHelloTo(firstName = "Jerry",lastName = "Aha")

Error example:

sayHelloTo(firstName = "Jerry","Aha")

2. When specifying parameter names, all parameters without default values ​​can be passed in.

Example:

sayHelloTo(firstName = "Jerry",lastName = "Aha")

3. When no parameter name is specified, it is necessary to distinguish the situation:

If all parameters with default values ​​are at the end when the function is defined, you can only pass in parameter values ​​without default values.

Example:

Function definition:

fun sayHelloTo(firstName: String, lastName: String = "Aha")

Correct example:

sayHelloTo("Jerry")

If the parameters with default values ​​are not all at the end when the function is defined, all parameter values ​​must be passed in.

Example:

Function definition:

fun sayHelloTo(firstName: String, middleName: String = "",lastName: String)

Correct example:

sayHelloTo("Jerry","","Aha")

Error example:

sayHelloTo("Jerry","Aha")

Demo Code

1、

class Person(
 val firstName: String = "Jerry",
 val lastName: String = "Yin"
) {
 fun sayHello() {
  ("Hi,$firstName $lastName")
 }

 fun sayHelloTo(firstName: String = "Baby", lastName: String = "Aha") {
  ("Hi,$firstName $lastName")
 }
}

2、

class Person2(
 val firstName: String = "Jerry",
 val lastName: String
) {
 fun sayHello() {
  ("Hi,$firstName $lastName")
 }

 fun sayHelloTo(firstName: String = "Baby", lastName: String) {
  ("Hi,$firstName $lastName")
 }
}

3. Call code:

Person().sayHello()
Person("Jerry1").sayHello()
Person("Jerry2", "Yin2").sayHello()
Person(firstName = "Jerry3").sayHello()
Person(lastName = "Yin4").sayHello()

Person().sayHelloTo()
Person().sayHelloTo("Baby1")
Person().sayHelloTo("Baby2", "Aha2")
Person().sayHelloTo(firstName = "Baby3")
Person().sayHelloTo(lastName = "Aha4")

Person2(lastName = "Yin5").sayHello()
Person2("Jerry6", "Yin6").sayHello()
Person2("", "").sayHelloTo(lastName = "Yin8")
Person2("", "").sayHelloTo("Jerry9", "Yin9")

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support.