Pick up Scala and Scala again

Source: Internet
Author: User

Pick up Scala and Scala again

Spark 1.2 has been released. You need to review Scala. Write a note called scala brief.

Scala runs on JVM and has a large number of class libraries and tools, both functional programming and object-oriented.

In Scala, the interpreter is our favorite REPL. The types of variables or functions are always written after variables or functions (opposite to java ), numeric type conversion uses methods instead of forced type conversion. You must use semicolons to separate multiple statements in the same line of code.

Scala allows user-defined operators. Note that scala prefixes can be omitted when using a pack starting with scala. Scala does not have static methods. Similar features can use Singleton objects. companion objects corresponding to a class are the same as static methods in Java, using the apply method of companion object is a common method for building objects in scala.

Control

In scala, the judgment statement is similar to other languages, but the {} block contains a series of expressions, and the result is also an expression. The value of the last expression in the block is the value of the block.

No val or var is specified before the for loop variable. The type of the variable is the element type of the set, and the scope of the Loop Variable continues until the end of the loop. Scala does not provide the break or continue statement to exit the loop. The alternative method is as follows: 1) Use the Boolean control variable 2) use nested functions -- return 3 in the function) using the break method in the Breaks object, the transfer of control is completed by throwing and capturing exceptions. For performance considerations, try to avoid this mechanism.

For has a rich array of forms. It can provide multiple generators and expressions starting with if. It can also be defined using any number of cyclic variables.

Function

Scala Methods Operate on objects, but functions are not. C ++ also has functions. However, in Java, intelligence can simulate using static methods. scala functions do not require return, the return type must be specified for the corresponding recursive function. You can mix unnamed parameters and with-name parameters, as long as those parameters are at the top.

Similarly, the variable length parameter list is very convenient, and the value sequence is converted to the parameter sequence through. When the variable length parameter type is the Object Java method, you need to manually convert the basic type.

Scala has a special representation of a function that does not return values. If the function body is contained in curly brackets but does not have the = sign before it, the return type is Unit. Such a function is also called a procedural function.

When val is declared as lazy, its initialization will be delayed until it is set to its initial value. Each time a variable is loaded with access latency, a method is called to check whether the value has been initialized in a thread-safe way.

Exception

Scala exceptions work in the same way as Java or C ++, but are not "checked" exceptions. The pattern matching syntax is used to capture exceptions, and no exception object needs to be captured, you can use _ to replace the variable name. Try/catch and try/finally are complementary.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.