scala方法中的变量
Scala变量 (Scala variables)
A variable is named a reference to a memory location. The location stores the data that is used by the program.
变量被称为对存储位置的引用。 该位置存储程序使用的数据。
Based on the data type of the variable the memory size allocated is defined.
根据变量的数据类型,定义分配的内存大小。
Scala variables are memory locations. Scala allows you to define variables of two types:
Scala变量是内存位置。 Scala允许您定义两种类型的变量:
Mutable Variables
可变变量
Immutable Variables
不变变量
1)可变变量 (1) Mutable Variables)
Variables that allow us to change its value any time in the code. Scala variables are created using the var keyword. You can optionally give the data type of the variable with data type name with first letter capital.
允许我们随时在代码中更改其值的变量 。 Scala变量是使用var关键字创建的。 您可以选择为变量的数据类型提供名称为大写字母的数据类型名称。
//Syntax with variable's data type
var variable_name : Data_type = value;
//Syntax without variable's data type
var variable_name = value;
Example:
例:
object MyClass {
def main(args: Array[String]) {
var a : Int = 33;
var b = 54;
a++;
println("variable declared with data type : " + a );
println("variable declared without data type : " + b );
}
}
Output
输出量
variable declared with data type : 34
variable declared without data type : 54
2)不可变变量 (2) Immutable Variables)
Variables that are made immutable are read-only variables in Scala. This means their value remains unchanged throughout the program. Scala variables are created using the val keyword. You can optionally give the data type of the variable with data type name with first letter capital.
使不可变的变量是Scala中的只读变量 。 这意味着它们的值在整个程序中保持不变。 Scala变量是使用val关键字创建的。 您可以选择为变量的数据类型提供名称为大写字母的数据类型名称。
//Syntax with variable's data type
val variable_name : Data_type = value;
//Syntax without variable's data type
val variable_name = value;
Example:
例:
object MyClass {
def main(args: Array[String]) {
val a : Int = 34;
val b = 54;
// a++; this is not allowed in this case
println("immutable variable declared with data type : " + a );
println("immutable variable declared without datatype : " + b );
}
}
Output
输出量
immutable variable declared with data type : 34
immutable variable declared without datatype : 54
3)变量的延迟初始化 (3) Lazy initialization of variables)
Lazy initialization of variables are those variables that are calculated when the first time they are accessed. In scala mutable variables cannot be lazy.
变量的延迟初始化是在首次访问它们时计算出的那些变量。 在scala中,可变变量不能是惰性的。
Only val i.e. immutable variable can make lazy. This means these variables are calculated only once.
只有val即不可变变量可以使延迟。 这意味着这些变量仅计算一次。
//Syntax with variable's data type
lazy val variable_name : Data_type = value;
//Syntax without variable's data type
lazy val variable_name = value;
Example:
例:
object MyClass {
def main(args: Array[String]) {
lazy val a : Int = 34;
val b = 54;
// a++; this is not allowed in this case
println("immutable variable declared with data type with lazy declaration : " + a );
println("immutable variable declared without datatype : " + b );
}
}
Output
输出量
immutable variable declared with data type with lazy declaration : 34
immutable variable declared without datatype : 54
翻译自: https://www.includehelp.com/scala/variables-in-scala.aspx
scala方法中的变量