2.Scala Foundation

Source: Internet
Author: User

Use of 1.Scala Repl

Install Scala and append the bin directory of the Scala installation directory to the PATH environment variable. In this case, you can enter Scala directly under CMD to enter REPL

Enter REPL, and you can enter a valid expression in Scala after the command prompt. For example, we enter 8*5+2

The above RES0 is the variable that holds the result of the expression, and int represents the data type of the expression result

Another example: The following is a simple definition of a Scala function literal to achieve two number of additions

In the above example, RES6 represents the function value corresponding to the function literal (the functor can be analogous to a class, appearing in the source code, the function value analogy is the object,

Appears at run time. (Int,int) =>int represents the type of the result (actual function2[int,int,int]).

The above-mentioned Res0 and RES6 can be used in subsequent expressions, such as: two number of quotient

Scala's REPL environment can use the TAB key for command completion, and you can use the UP and DOWN ARROW keys to scroll back and forth through the command history. As follows:

We call the method on the string "Hello", but do not remember the method name (or afraid to write the method name incorrectly) can be hit as the previous part of the method name,

Then press the TAB key to appear the method that matches the condition, we then hit C, then press TAB to complete the method. A character array that returns a string

Of course, Rhel also built in a lot of more useful commands, by typing: Help can see exactly what commands are built in

These commands are relatively simple. You can learn it by groping yourself in the REPL.

2. Variables

In the first section, we used the variable names generated automatically by REPL, such as RES0,RES6. In most cases we need to customize the variable name.

There are two types of variables in Scala: variable-type variables-declared with VAR, immutable type variables-declared with Val

For example, we use VAR to declare a variable: MVP currently holds this year's All-star mvp-Brooks

By the end of the regular season, MVP is likely to be in the library. So then we can re-assign the MVP, as follows:

The attentive reader may have noticed that when we defined the MVP, we did not give the MVP data type (which is bound to be an error in Java).

But Scala can run normally. This is because there is a mechanism for type inference in Scala. In most cases, the type of the variable can be inferred. In the code above

We directly assign the string to the MVP, obviously the MVP is the string type.

Of course we can manually specify the type of the variable, although it appears a bit more than once. The type of the specified variable can be done as follows:

The above code also does not include a semicolon at the end of the statement. In Scala, the semicolon at the end of the statement is also optional. Unless you write more than one statement in a row, you must

You need to add a semicolon to the end of the statement.

For an immutable type, we can declare the variable with Val, as follows:

The above code declares a Val variable answer and assigns an initial value to it. If the subsequent code assigns a value to the answer variable again, an error will be given.

Finally, the difference between Scala and Java in assigning values to multiple variables in an assignment statement. See Java First

In Java if we want to assign a value of 10 to X, Y, the syntax above will give an error. Because the above assignment in Java only assigns a value to Y.

In Java we can assign a value of 10 to X, y in the following way

And in Scala, let's try Val x,y=10 first.

From the running results, you can declare and assign an initial value to multiple variables in the same row in Scala

Try the Val x=y=10 in Scala:

From the running results can be seen in this point Scala will also error, the cause of the error is also Y1 no statement. Then we declare Y1 and continue to run this statement

The results are very interesting, so let's step through the analysis:

The first sentence is Var y1=11. Declaring Y1 and assigning values

The key is the second sentence, Val x1=y1=10. This statement Scala will do the splitting, first y1=10 10 to Y1, the emphasis is on the assignment statement in Scala

The return value is unit (different from Java). So that will lead to the final y1=10,x1= ()

3. Type

First look at a picture:

We're only looking at the red-framed part this time. This section corresponds to the basic data type (original type) in Java. Dashed lines represent the implicit conversions that can be made.

There is no original type in Scala. Even the int,double is a reference type.

For example, we can call methods on numbers, such as converting numbers and strings to each other, in Java we need to do this:

This conversion is still a bit chores in Java. Let's take a look at how the string-to-digital interaction is done in Scala.

You can see that this conversion is very simple in Scala. is also more in line with people's normal thinking.

Let's first look at line sixth: Since I is a reference type of type int in Scala, and the ToString method is overloaded, you can i.tostring

Take a look at line seventh: the string in Scala is the reuse of string in Java, and then the implicit conversion adds a lot to the string.

Function. We can see the implicit conversion of the app by clicking on the icon in front of line seventh, as follows:

That is to say, Str_i.toint is the Stringops method. Therefore, if a method of string is used in the future learning process, the method is

Not defined on string, this time do not forget to query the next Stringops method

In addition, when we do type transformation in Java, we often use coercion type conversions, such as:

In line seventh we convert the double type to the int type by forcing the type conversion.

The conversions between these types in Scala are done by means of a method, as follows:

In the sixth line of code above, we convert a double to an int by using the ToInt method provided by the double type.

In the above learning process, we know that the Stringops class provides many easy-to-use methods for the string class. Actually, the numeric types in Scala, such as int

Double, etc. also have richint,richdouble by implicit conversion to it added a lot of easy-to-use imagining, such as the To method

The To method above is the method of Richint.

4. Operators

In Scala, like + 、-、 *,/These operators are actually method calls, because these characters are valid identifiers in Scala.

In Scala, the + method of int is actually called, and eventually converted to 1.+ (2).

There is only one parameter method in Scala that can be written as the syntax of the operator, as follows:

Because both to and until are methods that have only one parameter, we can write the notation of the operator

And the operators have priority: in Scala, the priority is always greater than + +-

5.apply syntax

In Scala, there is an apply grammar sugar. The specific usage rules are as follows:

1. If obj () is called directly on the object obj. Then the compiler will convert to call Obj.apply ()

2. If Clazz () is called directly on the class Clazz. Then the compiler converts the Apply method to the associated object that calls Clazz

See the following example:

There is an implicit conversion in line six, and the final call is Stringops's apply method, and looking at Scala Doc can find a way to do this:

Line 8th is actually called the bigint's associated object's apply method, used to generate an instance of bigint, we look at the Scala doc, found:

It is found that the associated objects of the BigInt class contain many of the apply methods.

2.Scala Foundation

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.