Scala Getting Started Guide

Source: Internet
Author: User

Scala Interpreter

To start the scala interpreter, follow these steps:

● Install Scala.

● Make sure that the scala/bin directory is in the system path.

● Open the command line window in your operating system.

● Type Scala and press Enter.

Tip: Do you prefer command line? You can also run the scala interpreter in other ways. For more information, see http://horstmann.com/scala/install.

Now, type the command and press Enter. Each time, the interpreter displays the result. For example, if you type "8*5 + 2" (for example, the following bold text), you will get 42.

Scala> 8*5 + 2

Res0: Int = 42

The answer is named res0. You can use this name in subsequent operations:

Scala> 0.5 * res0

Res1. double = 21.0

Scala> "hello," + res0

RES2: Java. Lang. String = Hello, 42

As you can see, the interpreter also displays the type of the result-in this example, Int, double, and Java. Lang. String.

You can call a method. Depending on how the interpreter is started, you may be able to use the tab key to complete, instead of entering the method name completely. You can try to type res2.to and press the tab key. If the interpreter provides the following options:

Tochararray tolowercase tostring touppercase

It indicates that the tab key completion function is good. Next, type U and press the tab key again. You should be able to locate the individual completion as follows:

Res2.touppercase

Press enter to display the result. (If you cannot use the tab key in your environment, you have to enter the complete method name .)

Similarly, you can try to press the arrow keys "Forward" and "backward. In most implementations, you will see commands submitted before and can edit them. Use keys, → and del to change the previous command:

Res2.tolowercase

As you can see, the scala interpreter reads an expression, evaluates it, prints it out, and then reads the next expression. This process is called a read-evaluate-print-loop, that is, REPL.

Technically speaking, Scala is not an interpreter. What actually happens is that the entered content is quickly compiled into a bytecode, which is then executed by the Java Virtual Machine. For this reason, most Scala programmers prefer to call it "repl ".

Tip: repl is your friend. Instant feedback encourages us to try new things, and if it runs out of what you want, you will have a sense of accomplishment.

At the same time, opening an editor window is also a good idea, so that you can copy and paste successfully running code snippets for future use. Similarly, when you try a more complex example, you may want to organize it in the editor and then paste it to REPL.

Declared values and variables

In addition to using res0, RES1, and other names, you can also define your own names:

Scala> Val answer = 8*5 + 2

Answer: Int = 42

You can use these names in subsequent expressions:

Scala & gt; 0.5 * answer

RES3: Double = 21.0

The value defined with Val is actually a constant-you cannot change its content:

Scala> answer = 0

<Console>: 6: Error: reassignment to Val

To declare variables with variable values, you can use VaR:

VaR counter = 0

Counter = 1 // OK, we can change a VaR

In Scala, we encourage you to use Val-unless you really need to change its content. Java or C ++ programmers may be surprised that most programs do not need so many var variables.

Note that you do not need to provide the value or variable type. This information can be inferred from the expression you used to initialize it. (An error is reported if the value or variable is declared but not initialized .)

However, you can also specify the type when necessary. For example:

Val greeting: String = NULL

Val greeting: Any = "hello"

Note: In Scala, the types of variables or functions are always written after the names of variables or functions. This makes it easier for us to read statements of complex types.

When I switch back and forth between Scala and Java, I find that I often unconsciously declare the Java method, such as string greeting, which needs to be manually changed to greeting:
String. This is annoying, but whenever I face complicated Scala programs, I will be grateful because I don't need to interpret the C-style type declarations.

Note: we did not use semicolons after the variable declaration or value assignment statement. In Scala, you must use semicolons to separate multiple statements in the same line of code.

You can put multiple values or variables together for declaration:

Val xmax, Ymax = 100 //
Set xmax and Ymax to 100

VaR greeting, message: String = NULL

// Both greeting and message are strings and are initialized to null.

Common Types

So far, you have seen some Scala data types, such as int and double. Like java, scala also has seven numeric types: byte, Char, short, Int, long, float, and double, and 1 boolean type. Different from Java, these types are classes. Scala does not deliberately distinguish between basic types and reference types. You can perform the following operations on numbers:

1. tostring () // output string "1"

Alternatively, you can:

1. To (10) // output range (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

(We will introduce the range class in Chapter 13th. Now you just need to treat it as a set of numbers .)

In Scala, we do not need packaging types. The transformation between the basic type and the packaging type is the work of the scala compiler. For example, if you create an int array, an int [] array is obtained in the virtual machine.

As you can see in section 1.1, scala uses the underlying java. Lang. string class to represent strings. However, it adds hundreds of operations to strings through the stringops class.

For example, the Intersect method outputs a group of characters that are common to two strings:

"Hello". Intersect ("world ")//
Output "Lo"

In this expression, the Java. Lang. String object "hello" is implicitly converted into a stringops object, and then the Intersect method of the stringops class is applied.

Therefore, when using Scala documents, remember to take a look at the stringops class.

Scala also provides richint, richdouble, and richchar. They provide convenient methods that are not available to poor cousins, such as int, double, and char. The to method we used previously is actually the method in the richint class. In the expression

1. To (10)

In, int value 1 is first converted to richint, and then the to method is applied.

Finally, there are bigint and bigdecimal classes for numbers of any size (but poor. These classes correspond to Java. math. biginteger and Java. math. bigdecimal, however, as you can see in section 1.4, they are more convenient to use, and you can use regular mathematical operators to operate on them.

Note: In Scala, we use methods instead of forced type conversion to convert numeric types. For example, 99.44.toint gets 99, 99. tochar gets 'C '. Of course, like Java, tostring converts any object into a string.

To convert a string containing numbers to a value, use toint or todouble. For example, "99.44". todouble gets 99.44.

 

This article is excerpted from fast learning Scala.

Published by Electronic Industry Publishing House

(US) by horstmann (C. S .)

Translated by Gao Yuxiang

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.