The basic syntax is what Scala?

Last Xiaobian to introduce the features of the Scala language, this time we look at the basic syntax of Scala. If we have a Java-based, so learning Scala is also a relatively easy task.
Learning any language, we look at is its data type, Scala is no exception, we take a look at its data type is what. Scala data types are first divided into two categories: AnyVal and AnyRef. AnyVal corresponding to the basic data types in Java, AnyRef the corresponding reference data type in Java. Specifically AnyVal + 7 also includes two kinds: 7 types of numeric type, Byte, Short, Int, Long , Float, Double, Char; 2 Species non-numerical data types, Boolean, Unit. Unit which represents a null value, the equivalent of Java in the void. AnyRef including Scala in class, object and Trait (qualities). AnyVal and AnyRef there is a common parent, is the Any type.
Understanding of data types in Scala, Scala we look at the variables. We speak in front of the Scala features mentioned, the Scala declare variables can not earmarked for specific data types, or val unified with the var statement. Such as: var i = 3 or val i = 3. So the difference between var and val is, var statement is a variable, then the value can be changed. The val statement is equivalent to a constant, then the value can not be changed after the statement. Therefore, when using or val var declaration, as the case may be, if no special requirements, usually val statement.
Next, we look at the conditional expression in Scala. Conditional statement is similar to the Java language, syntax is as follows:The basic syntax is what Scala?

But there are several points to note are:

  1. There are conditional expression return value can be assigned to a variable. Such as:
    The basic syntax is what Scala?

  2. Conditional expression is determined by the type of the return value of the last expression of each branch. If the values ​​do not match each branch return type, it is used as the parent class of their minimum return type.

The above conditional statement, if assigned to a variable, the variable type can only adopt minimum parent String and Int, is the Any type.
Finally, we look at the loop in Scala. There loop for loop and while / do while loop. Using a loop through the array can be easily set, collection or tuples.
Scala has a lot in the for loop format. As shown, (assuming an array arr) below:
1) enhanced for loop
The basic syntax is what Scala?

2) using the index for loop
The basic syntax is what Scala?
3) for loop guarded
The basic syntax is what Scala?

4) add yield derivation for loop
The basic syntax is what Scala?

At this time, it will generate a new array, each array element value by 5 than previously.
While / do while loop in Scala, not much difference with the Java, in which we will not go into details. It should be noted that Scala in circulation is no break or continue statement, we need to add conditions to achieve similar functionality.
Well, this time we will introduce the basic syntax of Scala to this, and want to help you in learning Scala smooth sailing.
More knowledge you can click http://heze.offcn.com/, I hope to give you help!

Guess you like

Origin blog.51cto.com/14669527/2467954