scala语言示例_var关键字与Scala中的示例

scala语言示例

Scala var关键字 (Scala var keyword)

The var Keyword in scala is used to declare variables. As Scala does not require explicit declaration of data type of the variable, var keyword is used. The variable declared using the var keyword as mutable variables.

scala中var关键字用于声明变量。 由于Scala不需要显式声明变量数据类型,因此使用var关键字 。 使用var关键字声明的变量为可变变量。

Mutable Variables are those variable whose value can be changed in the program, these are used when you need variables which are updated as per the logic of the program. Example of some mutable variables are counters, loop variables, sum, etc.

可变变量是可以在程序中更改其值的那些变量,当您需要根据程序逻辑进行更新的变量时,将使用这些变量。 某些可变变量的示例包括计数器,循环变量,总和等。

Syntax to define mutable variables in Scala using var keyword:

使用var关键字在Scala中定义可变变量的语法:

    var I = 102;
    var H : string = "IncludeHelp";

Syntax explanation, in the first on we have declared a variable I using var keyword. This can be changed in future. The value is 102 so scala will itself make it an int type. In the second one we have explicitly declared the data type of the variable. This is a good practice and you know the limitation of the initialization. It declares the var H as a string using variable. The value will be strictly string and if we stored a number in it will be stored as a string.

语法说明,首先,我们使用var keyword声明了变量I。 将来可以更改。 值是102,因此scala本身会将其设置为int类型。 在第二篇文章中,我们明确声明了变量的数据类型。 这是一个好习惯,您知道初始化的局限性。 它使用变量将var H声明为字符串。 该值将严格为字符串,如果我们在其中存储了数字,则将其存储为字符串。

Using var keyword you can define variables of all data types in Scala. The value of variables declared using the var keyword can be changed at any point in the program.

使用var关键字,您可以在Scala中定义所有数据类型的变量。 使用var关键字声明的变量的值可以在程序的任何位置更改。

Scala程序演示var关键字示例 (Scala program to demonstrate example of var keyword)

// Program that displays usage of var keyword in Scala

object VarKeyword {
      def main(args: Array[String]) {
          var myVar = 52;//Variable Initialized with value 52 
          print("Value of my myVar =" + myVar + "\n")
          myVar = myVar + 6; // Value changes to 52+6
          print("Changed Value of myVar = " + myVar )
      }
   }

Output

输出量

Value of my myVar =52
Changed Value of myVar = 58

Example explanation:

示例说明:

The code displays use of var keyword in real world program. The variable myVar's value changes in the program.

该代码显示了在实际程序中var关键字的使用。 变量myVar的值在程序中更改。

翻译自: https://www.includehelp.com/scala/var-keyword-with-example-in-scala.aspx

scala语言示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值