In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you what is the use of Scala, I hope you will gain something after reading this article, let's discuss it together!
I. Foundation
1. When Scala declares values and variables, the value declared with val does not change what you give it, but var can. As follows:
Scala > val answer = 8answer: Int = 8
A value defined in val is actually a constant-- its contents cannot be changed.
If we want to declare variables whose values can be changed, we can use var:
Var counter = 0counter = 1 / / OK, we can change a var
2. There are some differences between Scala and Java when specifying variable types:
Java:
String greeting
Scala:
Val greeting: String = null
3. Scala7 numerical types: Byte, Char, Short, Int, Long, Float, Double, Boolean.
4. In Scala, we use methods instead of casting to convert between numeric types. For example: 99.44.toInt gets 99. 99. To Char gets'c'. Like Java, toString converts arbitrary objects into strings.
To convert a string containing a number to a numeric value, use toInt or toDouble. For example, "99.44" .toDouble gets 99.44.
5. When overloading arithmetic and operators in Scala, it is important to note that the operators in Scala are actually methods.
For example:
A + b
Is an abbreviation for the following method calls:
A. + (b)
The + here is the method name. You can use almost any symbol to name a method in Scala. In general, you can use:
A method b
As a shorthand for the following code:
a. Method (b)
The method here is a method with two parameters (one implicit and one explicit). For example:
1.to (10) can be written as: 1 to 10
6. Scala also has a significant difference. Scala does not provide + and-- operators, we need to use + = 1 or-= 1
7. The wildcard character in Scala is _ rather than java's *. For example, when introduced, import scala.math._
8. The apply method of Scala is a very common method. For example, if s is a string, then s (I) is the I th character of the string. In C++, we would write as s [I], in java, we would write as s.charAt (I). In Scala, we can just go like this: "Hello" (4) / / this will produce'o'. You can think of this method as an overloaded form of the () operator, which is implemented by a method called apply. For example, in the documentation of the StringOps class, there are the following methods:
Def apply (n: Int): Char
In other words, "Hello" (4) is an abbreviation of the following sentence:
"Hello" .apply (4)
9. Java programmers use Javadoc to browse Java API,Scala and have their own version, called Scaladoc.
Second, control structure and function
1. The if/else syntax structure of Scala is the same as Java or C++. However, it is worth noting that in Scala, the if/else expression has a value, which is the value of the expression that follows if or else. Such as:
If (x > 0) 1 else-1
The value of the above expression is 1 or-1, depending on the value of x. You can even assign the value of an if/else expression to a variable:
Val s = if (x > 0) 1 else-1
This has the same effect as the following statement:
If (x > 0) s = 1 else s =-1
However, the first is better because it is used to initialize a val, while in the second, s must be var.
After reading this article, I believe you have a certain understanding of "what is the use of Scala". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.