Scala Common Grammar Basic Grammar

1. Basic grammar

semicolon

Scala, like Java, needs to be marked with an English semicolon at the end of a line of code

type of data

insert image description here
Focus on the following data types:
For details, please refer to: https://www.scala-lang.org/files/archive/api/2.12.13/scala/

  1. Unit: Equivalent to void, often used in the return value of a function.
  2. Null: There is only one instance of null representing a null reference
  3. Any: It is the parent class of other classes, similar to Object in Java.
  4. AnyRef: is the parent class of all reference types, except for value types, all types inherit from AnyRef.
  5. AnyVal: It is the parent class of the value type. The value type contains 9 types, the numeric type scala.Double, scala.Float, scala.Long, scala.Int, scala.Char, scala.Short, scala.Byte, and the non-numeric type scala .Unit, scala.Boolean.

var and val

First of all, let me explain what a variable is. A variable is composed of variable name + variable value. The variable name is at the code level for developers to understand its specific meaning and easy to use, but at the computer storage level, it can be understood as stored in Where (that is, the address) on the computer storage medium
insert image description here

The variable a here points to the address Addr_2, which stores the value "abc".

Declaring a variable and initializing it is actually the process of allocating the value in memory and then pointing to the variable name.
It is clear that both var and val can be used to declare variables. The main difference is that the variables declared by var are mutable, and the variables declared by val are immutable, as in the following example

insert image description here
Among them, the variable a is marked with val, indicating that the object pointed to by a can only be 5 initialized at the time of declaration, and cannot be changed. The variable b is marked with var, indicating that b points to the 5 initialized at the time of declaration, but it can be changed in the future.

if - else statement

Basic Usage
insert image description herePrecautions:
When using the if - else statement, you need to remember that even when there is only one statement in the statement block, you can not write curly braces, but for the readability of the code, it must be in the statement block of each if statement in braces

loop statement

The basic need to be clear: where does the loop start? What is the step size for each cycle? Where does the loop end?

  1. while loop
    insert image description here
    The above is a standard while loop.

  2. do ... while loop
    insert image description here
    The above is a do...while loop, the statement in the do statement block must be executed once, you can guess what the output of the above program is

  3. Basic usage of for loop
    : use to or until:
    insert image description here

Both to and until have two modes, the difference is that the to loop finally includes end, while the until loop does not include end.
Note: When using a decimal as the start and end step, you need to use the BigDecimal type. The default Double type is inaccurate due to precision problems.

  • Advanced usage: loop filtering: add an if statement in the for loop, and implement the syntax of the filter
    when traversing the loop, such as: use yield to store the return value of the for loop as a new variable , such as:
    insert image description here

    insert image description here

2. Summary

The above is the common basic syntax of Scala, let’s learn about functions and implicit conversions in Scala

Guess you like

Origin blog.csdn.net/csdn_tiger1993/article/details/125590574