Scala learning-Basics

Source: Internet
Author: User
Tags mathematical functions

I. Basics

1. Variables
Val identifier: Declares constants. For example, Val answer = 1.
VaR flag: declares a variable;

  • Type inference: Scala deduce the type of a variable based on the expression of the initialization variable;

Val is encouraged;

Note: An error is reported if the variable is declared without initialization.

  • Explicit type: Scala variables or functions are always written after variables or functions.
1   val a:Int = 12   val str:String = 2

2. Common Types

Scala supports 7 numeric types and 1 boolean type.

  • Byte/Char/short/INT/long/float/double
  • Boolean

Note: All these types are classes, and Scala does not distinguish between basic types and reference types. For example, you can call the 1. tostring method.

3. numeric type conversion:

Scala uses a method instead of a forced type conversion to convert between numeric types. For example,

  

4. Operators

Scala supports the vast majority of operators in Java, but all operators are methods. For example:

A + B is the abbreviation of A. + (B)

That is, method a B is the abbreviation of method A (method B). The two methods can be exchanged.

[Note: Scala does not support ++ and -- operators .]

5. function calls and Methods

Common mathematical functions are included in the scala. Math package. You can use the following statement to introduce the package.

1 import scala. Math. _ // In Scala, the character _ is a wildcard, similar to * in Java *

In addition, Scala can be omitted when a pack starting with Scala is introduced or used.

1 import math._     2 3 math.sqrt(2) 

[Note: Scala does not have static methods .]

When calling a method without parameters, parentheses can be omitted, for example:

1     "hello World".toString

6. Apply Method

In Scala, elements such as arrays and linked lists are usually accessed in the form of function calls. For example:

In the preceding call, "hello" (0) is "hello". Apply (0. The apply method is a method in the stringops class.

 

Ii. control structures and functions

1. If statements also return values.

The IF statement is called a conditional expression, for example:

  

The structure of the conditional expression is:If (condition) value 1 else value 2The structure is similar to the three-object operator of Java,Scala does not support the three-object operator..

The conditional expression can be used to initialize the Val constant.

Note: Each expression in Scala returns a value, as shown in figure

  

The conditional expression returns (), where () indicates the unit class, indicating no value. It can be considered as the void type in Java.

2. Scala does not support switch statements, but Scala has a more powerful pattern matching mechanism.

3. Block expression

In Scala, {} is used to enclose a series of expressions. The {} is called a block expression. The value of a block expression is the value of the last expression in the block. For example,

, And the result is 7.

You can use a block expression to assign values to variables, for example:

  

The distance variable uses a block expression to assign values. Its value is the value of the last expression in the block.

4. the return value of the value assignment statement in Scala is unit. It can also be noted that the value assignment statement in Scala has no return value.

  

[Note: x = y = 3; this assignment is not supported in Scala, because the assignment statement has no return value in Scala. Therefore, the + =,-=, × =,/=, and other value assignment operators do not return values .]

5. Input and Output

  • Input:

Commonly used include Readline reading a line of input from the console, and Readline can also contain a prompt string, such:

  

Readint, readbyte, readdouble, readshort, readlong, readfloat, readboolean, and readchar are used to read values of their respective types.

  • Output:

Print (content), println (content), and printf; println includes line breaks. In addition, printf supports C-style string output, for example:

  

6. Cyclic operations

Scala supports the same while and do loops in Java:

  

Scala's for loop structure: (in a for loop, the variable is not preceded by the VaR or val identifier)

1 For (variable <-expression ){//Let the variable traverse all values of the expression2 3 // operation 4 5}

For example:

  

7. Advanced for loop usage

  • Multiple groups of "variables" <-expression "can be included in the for loop brackets. Groups are separated by semicolons, as shown in figure
1     for( i <- 1 until 3; j <- 1 until 3 ){ 2           println( i*j )      3     }

This structure of the For Loop is similar to the nested loop structure in Java.

In addition, you can use

1{I <-1 until 3 // use braces and use line breaks to separate group 2 j <-1 until 3}{3 4 println (I * j) 5}
  • You can add conditions for nested loops using the if expression:

  

If expression whether to add parentheses, the result does not change.

  

Until returns an interval that does not contain the upper limit, for example, 1 until 3 returns (1, 2 );

  

The to method returns a well-rounded interval, for example, 1 to 3 (1, 2, 3 );

  • For Derivation

If the for loop starts with yield, the for loop constructs a set and stores it in the set.

  

8. Scala does not support break and continue;

9. function-Scala supports functional programming.

1 def function name (parameter 1: Type 1, parameter 2: Type 2,...): Return Value Type = {2 3 // function body 4 5}

 

For example:

  

[Note: For functions with return values, the equal sign cannot be omitted; for recursive functions, the type of return values cannot be omitted .]

  

[Note: when defining a function without a return value, the equal sign can be omitted. The return type of a function without a return value is unit ;]

10. Functions with default values

  

Declarations with default values are similar to those with default values in Java.

11. Name-based parameter-specify the parameter and its corresponding value when calling a function

  

The first function call is a normal call, and the second function call is a call with a name parameter.

12. variable-length parameter list

After adding * to the type of the last parameter in the function parameter list, define a function that accepts any number of parameters.

  

13. In Scala, functions without return values are called procedures, as shown in.

During the process definition, there is no equal sign after the parameter list.

14,Lazy Value

But when Val is declared as lazy, initialization of the variable will be delayed until we take the value of the variable for the first time. For example:

1     lazy val fileContent = scala.io.Source.fromFile("filename").mkString

 

Only when we read the filecontent value will filecontent be initialized by reading the file. If the filecontent value is not read, the file will not be read.

15. Scala does not have a "checked" exception-some exception may be thrown if you do not need to declare a function or method.

 

Scala learning-Basics

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.