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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540603.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540603.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540603.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[轉載] python[1]-print中的sep、end參數

參考鏈接: Python | print()中的sep參數 讀示例程序代碼時遇到的問題,看不懂end和sep參數。經過查找,基本弄清楚了。 sep:可以設置print中分割不同值的形式。應該是separation的縮寫。 end:可以設置print打印結束時最…

分區 主分區 和 擴展分區_等和分區

分區 主分區 和 擴展分區Description: 描述: This is a popular interview coding problem which has been featured in interview rounds of Amazon, Oyo rooms, Adobe. 這是一個受歡迎的采訪編碼問題,已在亞馬遜,Oyo房間,Adobe…

ORACLE 物理讀 邏輯讀 一致性讀 當前模式讀總結淺析

在ORACLE數據庫中有物理讀(Physical Reads)、邏輯讀(Logical Reads)、一致性讀(Consistant Get)、當前模式讀(DB Block Gets)等諸多概念,如果不理解或混淆這些概念的話&a…

[轉載] Java Formatter toString()方法與示例

參考鏈接: Python | 輸出格式化 output format 格式化程序類toString()方法 (Formatter Class toString() method) toString() method is available in java.util package. toString()方法在java.util包中可用。 toString() method is for the string representat…

arm tbh_TBH的完整形式是什么?

arm tbhTBH:說實話 (TBH: To Be Honest) TBH is an abbreviation of "To Be Honest". It is internet slang which generally used as an acronym or hashtag over the internet on social media networking sites like Facebook, Instagram, Twitter, Yo…

異常:fatal: unable to access 'https://git.oschina.net/pcmpcs/library.git/': Could not resolve host...

git fork項目時出現的異常. 原因: 我以前用的是ssh地址做的遠程通信地址,而這次是用的是https,因為很久沒用,所以忘記了以前是用ssh的了。解決方案一:復制ssh協議的地址,然后再關聯遠程倉庫。并且在VCS下的git下的Rem…

計數器數組_子數組計數

計數器數組Problem statement: 問題陳述: Given an array of N positive integers a1, a2, ..., an. The value of each contiguous subarray of a given array is the maximum element present in that subarray. The task is to return the number of subarrays…

[轉載] 列表、元組及通用序列操作

參考鏈接: Python | 重點數據類型 (字符串,列表,元組,迭代)(String, List, Tuple, Iteration) 序列是Python中最基本的數據結構(一些基本特性類似于C中的數組模板類),序列中的每一個元素都有相…

onActivityResult()后onresume()

當你調用完一個存在的activity之后,onActivityResult將會返回以下數據:你調用時發出的requestCode、被調用activity的結果標志resultCode(如RESULT_OK)和其他的額外數據。我們期望的都是得到RESULT_OK,表示調用成功&am…

java反射用法示例_Java包| 類型,用法,示例

java反射用法示例配套 (Packages) Packages in Java is simply a mechanism to encapsulate (i.e. to put in a short and concise form) a group of classes,interfaces,enumerations, sub packages, etc. In real world, application is developed in such a manner so that …

[轉載] python 元組tuple - python基礎入門(14)

參考鏈接: Python元組Tuple 目錄 一.元組tuple定義 二.元組tuple查詢 三.元組tuple不支持刪除/修改數據 四.元組tuple與列表list的相互轉換 五.重點總結 在上一篇文章中我們講解了關于python列表List的相關內容,今天給大家解釋一下列表List的…

MaxCompute 2.0—從ODPS到MaxCompute

從ODPS到MaxCompute-阿里大數據的進化之路是一個商用大數據系統發展史,一個商業大數據系統要解決的問題有可靠性,高性能,安全性等等六個方面。內部產品名ODPS的MaxCompute,是阿里巴巴內部發展的一個高效能、低成本,完全…

python數值類型_Python數值類型

python數值類型In programming, Data Types are an essential concept. Data of various types can be stored in variables as per the task we want the variables to perform. 在編程中,數據類型是必不可少的概念。 根據我們希望變量執行的任務,各種類…

[轉載] Python高級變量(列表、元組、字典、字符串、公共方法)

參考鏈接: Python | 重點數據類型 (字符串,列表,元組,迭代)(String, List, Tuple, Iteration) 文章目錄 高級變量類型目標知識點回顧 01. 列表1.1 列表的定義1.2 列表常用操作del 關鍵字(科普)關鍵字、函數…

python 操作mongodb數據庫參考文檔

參考文檔鏈接:https://pypi.python.org/pypi/pymongo pymongo的參考文檔http://api.mongodb.com/python/current/tutorial.html mongoengine的參考文檔:https://pypi.python.org/pypi/mongoengine#downloads Flask-MongoEngine的參考文檔:htt…

php eot eod_EOD的完整形式是什么?

php eot eodEOD:一天結束 (EOD: End Of Day) EOD is an abbreviation of "End Of Day". EOD是“ End Of Day”的縮寫 。 It is an expression, which is commonly used in the Gmail platform. In a particular mail, if the sender wants to give the d…

[轉載] python元組 tuple

參考鏈接: Python元組Tuple 類型特點:可以存放多個、 可以重復的,有順序的數據,數據不可變。 如果項目中需要定義多個數據到一個變量中存放 存放的數據,在項目運行過程中,會發生數據的增加、修改、刪除…

aio nio aio_AIO的完整形式是什么?

aio nio aioAIO:多合一 (AIO: All-in-one) AIO is an abbreviation of "all-in-one", which is also known as an MFP (multi-function product/printer/peripheral), multi-functional or multi-function device (MFD). It is a workplace machine that …

[轉載] python基礎入門二

參考鏈接: Python集合Set 寫代碼,有如下變量,請按照要求實現每個功能 (共6分,每小題各0.5分) name ” aleX” 1)移除 name 變量對應的值兩邊的空格,并輸出處理結果 2) 判斷 name 變量對應的值是否以 “al” 開頭,并輸出結果?…

組合數據類型練習,英文詞頻統計實例上

1、字典實例:建立學生學號成績字典,做增刪改查遍歷操作。 建立: d{0001:99,0003:89,0004:98,0005:100,0006:78} 增:d[0002]79 刪:d.pop(0001) 改:d[0004]100 查:print(d[0002]) 遍歷操作&#x…