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方法中的變量