In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to declare Scala variables", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to declare Scala variables" bar!
A variable is an easy-to-use placeholder that refers to the computer's memory address, and it takes up a certain amount of memory space after it is created.
Based on the data type of the variable, the operating system allocates memory and decides what will be stored in reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or letters in these variables.
Variable declaration
Before we learn how to declare variables and constants, let's look at some variables and constants.
Variable: the amount whose value may change during the running of the program is called variable. Such as: time, age.
Second, the constant whose value will not change during the running of the program is called constant. Such as: numeric value 3, character'A'.
In Scala, use the keyword "var" to declare variables and the keyword "val" to declare constants.
An example of declaring a variable is as follows:
Var myVar: String = "Foo"
Var myVar: String = "Too"
The variable myVar is defined above and we can modify it.
An example of a declaration constant is as follows:
Val myVal: String = "Foo"
The constant myVal is defined above, which cannot be modified. If the program tries to change the value of the constant myVal, the program will make an error when compiling.
Variable type declaration
The type of the variable is declared before the equal sign after the variable name. The syntax format for defining the type of a variable is as follows:
Var VariableName: DataType [= Initial Value]
Or
Val VariableName: DataType [= Initial Value]
The variable declaration must require an initial value, otherwise an error will be reported.
Variable type reference
Declaring variables and constants in Scala does not necessarily specify the data type, which is inferred from the initial value of the variable or constant without specifying the data type.
Therefore, if you declare a variable or constant without specifying the data type, you must give its initial value, otherwise an error will be reported.
Var myVar = 10
Val myVal = "Hello, Scala!"
In the above example, myVar is inferred as Int and myVal is inferred as String.
Scala multiple variable declarations
Scala supports multiple variable declarations:
Val xmax, ymax = 100 / / xmax, ymax are all declared to be 100
If the return value of the method is a tuple, we can declare a tuple using val:
Scala > val pa = (40, "Foo")
Pa: (Int, String) = (40PowerFoo) Thank you for your reading, this is the content of "how to declare Scala variables". After the study of this article, I believe you have a deeper understanding of how to declare Scala variables, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.