Scala language Concise syntax

Source: Internet
Author: User
Tags constant scala ide

In the first article in this series, "thinking by using recursion," the authors do not introduce Scala's syntax first, for two reasons: one is because too much of the grammatical details distract the reader, ignoring the basic concept, the basic idea; and the second is that Scala The syntax is very concise, and programmers with other language programming experience can easily read Scala code. Now we're going to go back and start learning the Scala language from the basic syntax. You will find that Scala is exceptionally refined, and programs that implement the same functionality are typically less than half or more of the Java implementation in code, using Scala implementations. Dapper code often means easier to understand and easier to maintain. This article will introduce the basic syntax of the Scala language to help you write your first Scala program.

Development environment

The easiest way to learn Scala is to install a Scala IDE (integrated development environment), and the Typesafe company has developed an Eclipse based IDE. The IDE provides a handy feature for Scala beginners: worksheet. Like the Relp (Read-eval-print Loop) provided by Python or Ruby, worksheet allows users to enter Scala expressions and get the results of programs running immediately after they are saved, which makes it easy for users to experience the various features of the Scala language.

Hello World

Let's start with the classic Hello World program, just enter println in the worksheet ("Hello world!") Save, you can immediately see the program execution results on the right side of the statement.

Worksheet can also be used as a simple calculator, try to enter some formulas, save.

Figure 1. Worksheet

Variables and functions

Of course, we can't just satisfy the use of Scala for some arithmetic operations. To write a slightly more complex program, we need to define variables and functions. Scala provides two kinds of syntax for defining variables. Use Val to define a constant, and once defined, the variable name cannot be assigned a value. Use Var to define a variable that can be assigned a new value. In Scala, you are encouraged to use Val unless you have a clear need to use Var. For Java programmers, it may seem counterintuitive at first, but after you get used to it, you'll find that most of the times we don't need Var, a variable variable.

Listing 1. Defining variables

Val x = 0
var y = 1
    
y = 2//
assign a value to a constant there is a compile error
//x = 3 
    
//explicitly specified variable type
val x1:int = 0
var y1:int = 0

Looking at the code carefully, we'll have two discoveries:

Variable type not specified when defining variable. Does this mean Scala is the same dynamic type language as Python or Ruby? On the contrary, Scala is a strictly static type language, and because of its advanced type inference (type inference) technology, programmers do not need to explicitly specify types when writing programs, and the compiler infers type information based on context. For example, the variable x is assigned to 0,0 as an integral type, so the type of x is inferred as an integral type. Of course, the Scala language also allows you to display a specified type, such as a variable x1,y1 definition. In general, we should try to use the type inference system provided by Scala to make the code look more concise.

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.