日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/11 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 scala方法中的变量_Scala中的变量 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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.

變量被稱為對(duì)存儲(chǔ)位置的引用。 該位置存儲(chǔ)程序使用的數(shù)據(jù)。

Based on the data type of the variable the memory size allocated is defined.

根據(jù)變量的數(shù)據(jù)類型,定義分配的內(nèi)存大小。

Scala variables are memory locations. Scala allows you to define variables of two types:

Scala變量是內(nèi)存位置。 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.

    允許我們隨時(shí)在代碼中更改其值的變量 。 Scala變量是使用var關(guān)鍵字創(chuàng)建的。 您可以選擇為變量的數(shù)據(jù)類型提供名稱為大寫字母的數(shù)據(jù)類型名稱。

    //Syntax with variable's data typevar variable_name : Data_type = value;//Syntax without variable's data typevar 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中的只讀變量 。 這意味著它們的值在整個(gè)程序中保持不變。 Scala變量是使用val關(guān)鍵字創(chuàng)建的。 您可以選擇為變量的數(shù)據(jù)類型提供名稱為大寫字母的數(shù)據(jù)類型名稱。

    //Syntax with variable's data type val variable_name : Data_type = value;//Syntax without variable's data typeval 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 caseprintln("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.

    變量的延遲初始化是在首次訪問它們時(shí)計(jì)算出的那些變量。 在scala中,可變變量不能是惰性的。

    Only val i.e. immutable variable can make lazy. This means these variables are calculated only once.

    只有val即不可變變量可以使延遲。 這意味著這些變量僅計(jì)算一次。

    //Syntax with variable's data typelazy val variable_name : Data_type = value;//Syntax without variable's data typelazy 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 caseprintln("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方法中的變量

    總結(jié)

    以上是生活随笔為你收集整理的scala方法中的变量_Scala中的变量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。