scala方法中的变量_Scala中的变量

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允许您定义两种类型的变量:

  1. Mutable Variables

    可变变量

  2. 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方法中的变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值