Spark basic study notes: Scala variables and data types

1. Variable declaration

(1) Brief description

Variables are declared in Scala using the keywords val and var. val is similar to a final variable in Java, that is, a constant, which cannot be modified once initialized; var is similar to a non-final variable in Java, which can be assigned and modified multiple times.
val - value - value
var - variable - variable

(2) Use val to declare variables

1. Declaration method

val <variable name>[: data type] = variable value It
is more flexible to define variables, you can specify the data type, or you can not specify the data type, and Scala can judge the data type by itself according to the assigned value. In declaring variables,
Scala has both Java and Features of Python. Java is a static language, declaring variables must specify the data type, Python is a dynamic language, declaring variables does not need to specify the data type.

2. Case demonstration

insert image description here
insert image description here

(3) Use var to declare variables

1. Declaration method

var <variable name>[:datatype] = variable value

2. Case demonstration

insert image description here

insert image description here

(4) Newline input statement (continued line)

insert image description here
If you want to enter a multi-line string, it is impossible to use double quotes, you have to use triple quotes

insert image description here

(5) Declare multiple variables at the same time

insert image description here
Scala cannot assign different values ​​to multiple variables at once
insert image description here

(6) Summary variable declaration

1. Variables must be initialized

insert image description here

2. Define variables without specifying the data type

insert image description here

3. Encourage the priority use of val (constant)

Scala encourages the use of val (constant) in preference to var unless it is really necessary to modify it.

4. The statement does not need to write the terminator

insert image description here

2. Data type

(1) Scala type hierarchy

1. Any type

Any is the root of the Scala class hierarchy, also known as a superclass or top-level class. Every class in the Scala execution environment inherits from this class, directly or indirectly. Some common methods are defined in this class, such as equals(), hashCode() and toString(). Any has two direct subclasses: AnyVal and AnyRef

2. AnyVal type

AnyVal represents a value type, and there are 9 predefined value types, which are non-empty Double, Float, Long, Int, Short, Byte, Char, Unit, and Boolean. Unit is a value type that does not contain any information, equivalent to void in the Java language, and is used as the result type of a method that does not return any results. Unit has only one instance value, written as ().

3. AnyRef type

AnyRef represents a reference type. All non-value types are defined as reference types. Every user-defined type in Scala is a subtype of AnyRef. AnyRef corresponds to java.lang.Object in Java.

4. Case demonstration

Define a variable list whose type is List[Any]. The list includes strings, integers, characters, Boolean values ​​and functions. Since these elements belong to the instance of the object Any, they can be added to the list.
insert image description here
Through the enhanced for loop to traverse the list elements and display their data types

scala> list.foreach(x => println(x + ": " x.getClass.getSimpleName))
love:String
100:Integer
66.6:Double
c:Charater
true: Bootlean
<function0>: anonfun$1

Get the sublist of the first three elements of the list
insert image description here

scala> list.reverse
res6:List[Any] = List(<function0>, true, c, 66.6, 100, love)

scala>List
res7: List[Any] = List(love, 100, 66.6, c, true, <function0>)

(2) Basic data types

1. Basic type table

insert image description here

2. Brief description

(1) String is under the java.lang package, and other types are under the scala package. Since Scala will automatically import java.lang and scala packages, these types can be used directly in the program.
(2) In fact, there is no real basic type in scala (such as the basic type of Java), and the above ten basic types are all classes.
(3) In Scala, the difference between the basic data type and other classes is that its objects are embodied in the form of direct quantity, which is basically the same as that in Java.
(4) Usage of """ of the String type: the syntax of """...""" can be used in the String literal to directly include a piece of content, so that the content can contain any character without escaping. At this time You can use the pipe character | to control the indentation format before each line, and call stripMargin on the string to align the string at |.Use
insert image description here
.stripMargin and the pipe character "|" to adjust the string output format

insert image description here

3. Basic types of rich wrappers

The methods provided by the nine basic types are relatively limited. In order to expand its functions, Scala provides corresponding nine rich wrappers, which are actually nine classes. The literals of these nine classes can be automatically converted to their corresponding rich wrappers when needed. Wrapper classes to call extra methods provided by rich wrapper classes.

(3) Data type conversion

1. Conversion of value types

Value types in Scala can be converted as shown in the figure below, and the conversion is one-way.
insert image description here
Convert Long to Float. Note that some precision will be lost in this case.
insert image description here
If you convert Float to Long, an error will be reported.

insert image description here
It cannot be automatically converted from Float type to Long type, but it can be implemented by calling a method

insert image description here

2. Conversion of reference types

Additionally, Scala can convert reference types to their subtypes.
Nothing is a subclass of all types, similar to the identifying interface in Java (such as serializable, which is used to indicate that the class can be serialized). For example, if a method throws an exception, the return value type of the exception is Nothing (although will not return).
Null is a subclass of all reference types (AnyRef), so Null can be assigned to all reference types, but not to value types. This is the same semantics as Java. Null has a unique single column value of null.
insert image description here

Guess you like

Origin blog.csdn.net/py20010218/article/details/125341417