Kotlin 简明教程
Kotlin - Variables
变量是任何编程中重要的一部分。它们是变量在这种变量中储存值并稍后使用这些名称来获取储存的值并将它们用于你的程序。
Variables are an important part of any programming. They are the names you give to computer memory locations which are used to store values in a computer program and later you use those names to retrieve the stored values and use them in your program.
Kotlin 变量或者使用 var 或 val 关键词创建并随后使用等号 = 为这些创建的变量赋值。
Kotlin variables are created using either var or val keywords and then an equal sign = is used to assign a value to those created variables.
Syntax
以下是创建两个变量并随后为他们赋值的不同值的简单语法:
Following is a simple syntax to create two variables and then assign them different values:
var name = "Zara Ali"
var age = 19
var height = 5.2
Examples
一旦创建一个变量并为其赋值,稍后我们就可以使用其名称访问它的值如下:
Once a variable is created and assigned a value, later we can access its value using its name as follows:
fun main() {
var name = "Zara Ali"
var age = 19
println(name)
println(age)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Zara Ali
19
我们再来看一个使用美元符号 $ 访问变量值的示例:
Let’s see one more example where we will access variable values using dollar sign $:
fun main() {
var name = "Zara Ali"
var age = 19
println("Name = $name")
println("Age = $age")
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Name = Zara Ali
Age = 19
我们再来看一个不使用美元符号显示变量值的示例如下:
Let’s see one more example to display the variable values without using a dollar sign as below:
fun main() {
var name = "Zara Ali"
var age = 19
println("Name = " + name)
println("Age = " + age)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Name = Zara Ali
Age = 19
Kotlin Mutable Variables
易变意味着变量可以在初始赋值后重新赋值为不同的值。若要声明一个易变变量,我们使用 var 关键词,就像我们在上述示例中使用的那样:
Mutable means that the variable can be reassigned to a different value after initial assignment. To declare a mutable variable, we use the var keyword as we have used in the above examples:
fun main() {
var name = "Zara Ali"
var age = 19
println("Name = $name")
println("Age = $age")
name = "Nuha Ali"
age = 11
println("Name = $name")
println("Age = $age")
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Name = Zara Ali
Age = 19
Name = Nuha Ali
Age = 11
Kotlin Read-only Variables
只读变量可以使用 val (而不是 var)声明,一旦赋值,其值无法再次被赋值。
A read-only variable can be declared using val (instead of var) and once a value is assigned, it can not be re-assigned.
fun main() {
val name = "Zara Ali"
val age = 19
println("Name = $name")
println("Age = $age")
name = "Nuha Ali" // Not allowed, throws an exception
age = 11
println("Name = $name")
println("Age = $age")
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
main.kt:8:4: error: val cannot be reassigned
name = "Nuha Ali" // Not allowed, throws an exception
^
main.kt:9:4: error: val cannot be reassigned
age = 11
^
Read-only vs Mutable
Mutable 变量将用于定义变量,它会根据程序执行过程中的不同条件持续改变其值。
The Mutable variables will be used to define variables, which will keep charging their values based on different conditions during program execution.
你将使用 Read-only 变量定义不同的常量值即在整个程序期间保留其值的变量。
You will use Read-only variable to define different constant values i.e. the variables which will retain their value throughout of the program.
Kotlin Variable Types
Kotlin 足够智能,可以识别 “Zara Ali” 是一个字符串并且 19 是一个数字变量。然而,你可以在创建它时明确指定变量类型:
Kotlin is smart enough to recognise that "Zara Ali" is a string, and that 19 is a number variable. However, you can explicitly specify a variable type while creating it:
fun main() {
var name: String = "Zara Ali"
var age: Int = 19
println("Name = $name")
println("Age = $age")
name = "Nuha Ali"
age = 11
println("Name = $name")
println("Age = $age")
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Name = Zara Ali
Age = 19
Name = Nuha Ali
Age = 11
我们很快将了解 Kotlin 中可用于创建不同类型变量的不同数据类型。
Soon we will learn more about different data types available in Kotlin which can be used to create different type of variables.
Kotlin Variable Naming Rules
在对 Kotlin 变量命名时需要遵守一些规则:
There are certain rules to be followed while naming the Kotlin variables:
-
Kotlin variable names can contain letters, digits, underscores, and dollar signs.
-
Kotlin variable names should start with a letter or underscores
-
Kotlin variables are case sensitive which means Zara and ZARA are two different variables.
-
Kotlin variable can not have any white space or other control characters.
-
Kotlin variable can not have names like var, val, String, Int because they are reserved keywords in Kotlin.