Getting Started with Kotlin: Variables and Functions - 02

Table of contents

1. Kotlin basic data types

Edit

 2. Variables

val keyword: 

var keyword:

 Type inference:

Nullable types: 

3. Function 

Basic function syntax: 

 Single expression function:

Default parameter values: 

Named parameters:

1. Kotlin basic data types

        Kotlin's basic numerical types include Byte, Short, Int, Long, Float, Double, etc. Unlike Java, characters do not belong to numeric types and are an independent data type. 

 2. Variables

        When defining a variable in Kotlin, only two keywords are allowed to be declared before the variable: val and var .

val keyword: 

  •  Val (short for value) is used to declare an immutable variable. This variable can no longer be reassigned after the initial assignment, corresponding to the final variable in Java.
val myConstant: Int = 10

var keyword:

  •  Var (short for variable) is used to declare a mutable variable. This variable can still be reassigned after the initial assignment, corresponding to non-final variables in Java.
var myVariable: String = "Hello, World!"
myVariable = "Kotlin is awesome!"

 Type inference:

         Kotlin has a type inference function that can automatically deduce the type of a variable based on its initialization value. For example:

var name = "John" // 推断为 String 类型
val age = 25      // 推断为 Int 类型

Nullable types: 

        Kotlin also introduced the concept of nullable types, allowing variables to accept null values. Declare a nullable type by appending it ?to . For example:

var nullableValue: String? = null

3. Function 

Basic function syntax: 

The following function is the summation of variables a,b 

// 定义一个函数,接受两个整数参数并返回它们的和
fun addNumbers(a: Int, b: Int): Int {
    return a + b
}

// 调用函数并打印结果
val result = addNumbers(5, 3)
println("The result is $result")
  • fun: Keywords that define functions.
  • addNumbers: Function name, which can be customized according to needs. It is best to clearly describe the function of the function.
  • (a: Int, b: Int): Declare function parameters within parentheses. In this example, the function accepts two arguments of type integer named  a and  b.
  • : Int:: The part after ( ) represents the return type of the function. In this example, the function returns a value of type integer.
  • { return a + b }: Inside the curly brackets is the function body, which contains the specific logic of the function. In this example, the function body simply adds the arguments  a and  b values ​​and  return returns the result via the keyword.

 Single expression function:

        If the function body contains only one expression, you can use single-expression functions to simplify your code. For example, the above addNumbersfunction can be simplified to:

fun addNumbers(a: Int, b: Int) = a + b

Default parameter values: 

        Kotlin allows setting default values ​​for parameters in function definitions. This way, parameters with default values ​​can be omitted when calling the function. For example:

fun greet(name: String = "Guest") {
    println("Hello, $name!")
}

greet()          // 输出:Hello, Guest!
greet("John")    // 输出:Hello, John!

Named parameters: 

        When calling a function, you can use named parameters to specify the values ​​of specific parameters and pass parameters in any order. For example:

fun printPersonInfo(name: String, age: Int, city: String) {
    println("Name: $name, Age: $age, City: $city")
}

printPersonInfo(age = 25, name = "John", city = "New York")

Guess you like

Origin blog.csdn.net/A125679880/article/details/132265725