A. Variables and basic data structures

Source: Internet
Author: User

1. There are two types of variables in Scala: mutable variables and immutable variables, variable variables using the keyword var definition, immutable variables defined by Val;

Variables defined in 2.Scala must initialize the assignment, you can use the placeholder (_) to initialize, the base type string using "_" after initialization is null,float/double/int after initialization for 0,char after initialization? (Note: Placeholders cannot be used in the method declaration);

3. Variables that use the keyword lazy modifier are assigned only when they are actually used, but lazy can only modify a Val-type variable (to be re-assigned if the variable is not used while the program is running);

4.Float type data must be appended with f/f after the floating-point number, otherwise it will be deduced as double by type;

5.Double types of data can be expressed as an exponent, for example: 0.31415e1 = = 3.1415;

The 6.Char type is wrapped in single quotes, with special characters using the escape character (\) or Unicode encoding;

7.String type use three double quotation marks ("" ") package can be output as is;

The 8.Scala provides a +,-symbol to represent positive negative numbers, and can be used directly for calculation;

9.Scala Use = = comparison is based on content rather than references!!! For example: "hi" = = new String ("HI") and 1 = = 1.0 all return true, but 1 = = "1" returns false;

The String defined in 10.Scala is based on java.lang.String, so the methods in Java.lang.String are available, and some new methods are encapsulated in implicit conversions. For example: reverse, map, drop, slice and so on;

11. Operator precedence is similar to Java, where there are multiple priority operations performed from left to right, but::(list constructs) and::(list concatenation) is an exception! ;

12. Tuples can put different types in the same variable, through the variable name. _n (N is the index number, starting from 1), you can also extract the contents of the tuple and initialize the variables. For example: 1. var tuple = ("Hello", ' Cheng ', 1), tuple._1 = "Hello", 2. Val (first,second,third) = tuple,first:string = Hello,second:string = Cheng;

13. Symbol type definition need to use ' symbol, mainly play the role of identification, re-match, content judgment is more commonly used;

A. Variables and basic data structures

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.