Introduction to Scala -- data types

Keywords: Scala Big Data

1. Output statements and semicolons

1.1 output statement

Mode 1: line feed output

format:  println(Write the data you want to print to the console);

Mode 2: output without line feed

format: print(Write the data you want to print to the console);

Note: multiple values can be printed at the same time regardless of println() or print() statements. The format is: println (value 1, value 2, value 3...)

1.2 semicolon

In Scala statements, the semicolon at the end of a single line of code can be written or not written. If multiple lines of code are written on one line, the semicolon in the middle cannot be omitted, and the semicolon of the last code can be omitted or not written

Example:

println("Hello, Scala!")			//The last semicolon can be written or not
//If multiple lines of code are written on one line, the semicolon of the preceding statement must be written, and the semicolon of the last statement can be omitted
println("Hello"); println("Scala")	

2. Constants in Scala

2.1 general

A constant is an amount whose value cannot be changed during the operation of a program

2.2 classification

  • Literal constant (the following are commonly used)
    • integer constants
    • Floating-point constants
    • character constants
    • string constant
    • Boolean Literals
    • Null constant
  • Custom constants (explained later)

2.3 code demonstration

//integer constants 
println(10)
//Floating-point constants 
println(10.3)
//Character constants, values should be enclosed in single quotes
println('a')
//String constant, the value should be enclosed in double quotes
println("abc")
//Boolean constant with only true and false values
println(true)
//Null constant
println(null)

3. Variables in Scala

3.1 general

We will define variables every day when we write scala programs. What is a variable and how is it defined?

Variable refers to the amount whose value can be changed during the execution of the program. The definition format is as follows:

3.2 syntax format

Java variable definition

int a = 0;

In scala, you can use val or var to define variables. The syntax format is as follows:

val/var Variable name:Variable type = Initial value

among

  • val defines variables that cannot be reassigned, that is, custom constants
  • var defines a variable that can be re assigned

Note: when defining variables in scala, the type is written after the variable name

3.3 example

**Requirements: * * define a variable to save a person's name "tom"

step

  1. Open the scala interpreter
  2. Define a variable of string type to hold the name

Reference code

scala> val name:String = "tom"
name: String = tom

3.4 difference between Val and var variables

Example

Re assign the name variable to Jim and observe its running results

Reference code

scala> name = "Jim"
<console>:12: error: reassignment to val
       name = "Jim"

Example

Use var to redefine the variable to save the name "tom", and try to reassign it to Jim to observe its running results

Reference code

scala> var name:String = "tom"
name: String = tom

scala> name = "Jim"
name: String = Jim

Note: val is preferred to define variables. If variables need to be re assigned, var is used

3.5 using type inference to define variables

scala's syntax is simpler than Java, and we can use a more concise way to define variables.

Example

Use a more concise syntax to define a variable to hold a person's name "tom"

Reference code

scala> val name = "tom"
name: String = tom

scala can automatically infer the type of variable according to the value of variable, which makes writing code more concise.

4. String

scala provides a variety of ways to define strings. In the future, we can choose the most convenient way to define strings according to our needs.

  • Use double quotation marks
  • Use interpolation expressions
  • Use three quotation marks

4.1 use double quotation marks

grammar

val/var Variable name = ""String"

Example

There is a person whose name is "hadoop". Please print his name and the length of his name.

Reference code

scala> println(name + name.length)
hadoop6

4.2 using interpolation expressions

In scala, interpolation expressions can be used to define strings, effectively avoiding the splicing of a large number of strings.

grammar

val/var Variable name = s"${variable/expression}character string"

be careful:

  • Add s before defining string
  • In strings, ${} can be used to reference variables or write expressions

Example

Please define several variables, save "zhangsan", "23" and "male" respectively, and define a string to save these information.

Printout: name=zhangsan, age=23, sex=male

Reference code

scala> val name = "zhangsan"
name: String = zhangsan

scala> val age = 23
age: Int = 23

scala> val sex = "male"
sex: String = male

scala> val result = s"name=${name}, age=${age}, sex=${sex}"
result: String = name=zhangsan, age=23, sex=male

scala> println(result)
name=zhangsan, age=23, sex=male

4.3 use three quotation marks

If you have large pieces of text to save, you can use three quotation marks to define the string. For example: save a large segment of SQL statements. Everything in the middle of the three quotation marks will be the value of the string.

grammar

val/var Variable name = """String 1
 String 2"""

Example

Define a string and save the following SQL statement

select
	*
from
    t_user
where
    name = "zhangsan"

Print the SQL statement

Reference code

val sql = """select
     | *
     | from
     |     t_user
     | where
     |     name = "zhangsan""""

println(sql)

4.4 extension: lazy assignment

In the big data development of enterprises, sometimes very complex SQL statements are written, which may have hundreds or even thousands of lines. If these SQL statements are directly loaded into the JVM, there will be a lot of memory overhead. How to solve this problem?

When some variables save large data, and these data do not need to be loaded into the JVM memory immediately. You can use lazy assignment to improve efficiency.

Syntax format:

lazy val/var Variable name = expression

Example

The following complex SQL statement needs to be executed in the program. We hope to load it only when we use this SQL statement.

"""insert overwrite table adm.itcast_adm_personas
    select
    a.user_id,
    a.user_name,
    a.user_sex,
    a.user_birthday,
    a.user_age,
    a.constellation,
    a.province,
    a.city,
    a.city_level,
    a.hex_mail,
    a.op_mail,
    a.hex_phone,
    a.fore_phone,
    a.figure_model,
    a.stature_model,
    b.first_order_time,
    b.last_order_time,
      ...
    d.month1_hour025_cnt,
    d.month1_hour627_cnt,
    d.month1_hour829_cnt,
    d.month1_hour10212_cnt,
    d.month1_hour13214_cnt,
    d.month1_hour15217_cnt,
    d.month1_hour18219_cnt,
    d.month1_hour20221_cnt,
    d.month1_hour22223_cnt
    from gdm.itcast_gdm_user_basic a
      left join gdm.itcast_gdm_user_consume_order b on a.user_id=b.user_id
    left join gdm.itcast_gdm_user_buy_category c on a.user_id=c.user_id
    left join gdm.itcast_gdm_user_visit d on a.user_id=d.user_id;"""

Reference code

scala> lazy val sql = """insert overwrite table adm.itcast_adm_personas
     |     select
     |     a.user_id,
	....
     |     left join gdm.itcast_gdm_user_buy_category c on a.user_id=c.user_id
     |     left join gdm.itcast_gdm_user_visit d on a.user_id=d.user_id;"""
sql: String = <lazy>

5. Identifier

5.1 general

In actual development, we will write a lot of code. There must be variables, methods, classes, etc. How should they be named? This requires identifiers. Identifiers are used to name variables, methods, classes, etc. identifiers in Scala are very similar to those in Java

5.2 naming rules

  • It must consist of upper and lower case English letters, numbers, underscores, Dollar sign $, any combination of these four parts
  • A number cannot begin
  • Cannot have the same name as a keyword in Scala
  • You'd better know what you mean

5.3 naming conventions

  • Variable or method: starting from the second word, the first letter of each word is capitalized, and all other letters are lowercase (small hump naming method)

    zhangSanAge, student_Country, getSum
    
  • Class or trait: the first letter of each word is capitalized, and all other letters are lowercase (big hump nomenclature)

    Person, StudentDemo, OrderItems
    
  • Package: all lowercase, usually the company's domain name is backfilled, and multi-level packages are separated by

    com.itheima.add,  cn.itcast.update
    

6. Data type

6.1 brief description

Data types are used to constrain the value range of variables (constants). Scala is also a strongly typed language. Most of the data types in it are the same as Java. Let's mainly learn

  • Some usage different from Java
  • Inheritance system of data types in scala

6.2 data type

Foundation typeType description
Byte8-bit signed integer
Short16 bit signed integer
Int32-bit signed integer
Long64 bit signed integer
Char16 bit unsigned Unicode character
StringChar type sequence (string)
Float32-bit single precision floating-point number
Double64 bit double precision floating point number
Booleantrue or false

Note the difference between scala types and Java

[!NOTE]

  1. All types in scala begin with uppercase letters
  2. Shaping uses Int instead of Integer
  3. Variables defined in scala can be typed without writing, so that the scala compiler can infer automatically
  4. In Scala, the default integer is Int, and the default floating-point type is Double

6.3 Scala type hierarchy

typeexplain
AnyAll types of parent classes, which have two subclasses AnyRef and AnyVal
AnyValParent of all numeric types
AnyRef**Parent of all object types (reference types) * *
UnitUnit is a subclass of AnyVal. It has only one instance {% EM%} () {% endem%}. It is similar to void in Java, but scala is more object-oriented than Java
NullNull is a subclass of AnyRef, that is, it is a subclass of all reference types. Its instance is {% EM%} null {% endem%}, which can assign null to any object type
NothingSubclasses of all types cannot directly create instances of this type. When a method throws an exception, it returns Nothing. Because Nothing is a subclass of all classes, it can be assigned to any type

6.4 thinking questions

Is there a problem with the following code?

val b:Int = null

Scala will explain the error: Null type cannot be converted to Int type, indicating that Null type is not a subclass of Int type

7. Type conversion

7.1 general

When a Scala program performs an operation or assignment, the data type value with a small range will be automatically converted to a data type value with a large range, and then calculated. For example, the operation result of 1 + 1.1 is 2.1 of a Double type. Sometimes, we will involve actions similar to "rounding", It is necessary to convert a decimal to an integer for calculation. These contents are the type conversion in scala

Type conversion in Scala can be divided into value type conversion and reference type conversion. Here we focus on value type conversion

The type conversion of value type is divided into:

  • Automatic type conversion
  • Cast type

7.2 automatic type conversion

  1. explain

    Data type values with a small range will be automatically converted to data type values with a large range. This action is called automatic type conversion

    Automatic type conversion from small to large: byte, short, char - > int - > long - > float - > double

  2. Sample code

    val a:Int = 3
    val b:Double = 3 + 2.21	//Because the values of int type and Double type are calculated, the final result is: Double type
    val c:Byte = a + 1	//This will result in an error, because the final calculation result is Int type data, and it is certainly not possible to assign it to Byte type
    

7.3 cast type

  1. explain

    A wide range of data type values can be converted into a small range of data type values through a certain format (cast function). This action is called cast

    Note: the use of cast may cause the problem of missing precision!

  2. format

val/var Variable name:data type = Specific value.toXxx		//Xxx indicates the data type you want to convert to
  1. Reference code
val a:Double = 5.21
val b:Int = a.toInt

7.4 conversion between value type and String type

1. Convert data of value type to String type

Format I:

val/var Variable name:String = Value type data + ""

Format 2:

val/var Variable name:String = Value type data.toString

Example

Convert int, double and Boolean data into its corresponding string form

Reference code:

val a1:Int = 10
val b1:Double = 2.1
val c1:Boolean = true

//Method 1: it is realized by splicing with an empty string
val a2:String = a1 + ""
val b2:String = b1 + ""
val c2:String = c1 + ""

//Method 2: implemented by toString function
val a3:String = a1.toString
val b3:String = b1.toString
val c3:String = c1.toString

2. Convert the data of string type to its corresponding value type

Format:

val/var Variable name:Value type = String value.toXxx	//Xxx indicates the data type you want to convert to

be careful:

  • The method of converting String type data into Char type data is a little special. Instead of calling toChar, toCharArray is called
  • This point can be understood first, and we will explain it in detail later

Requirements:

Convert string type integer, floating point number and Boolean data to its corresponding value type data

Reference code:

val s1:String = "100"
val s2:String = "2.3"
val s3:String = "false"

//Convert string type data to its corresponding Int type
val a:Int = s1.toInt
//Convert the data of string type to its corresponding Double type
val b:Double = s2.toDouble
//Convert string type data to its corresponding Boolean type
val c:Boolean = s3.toBoolean

Posted by theresandy on Tue, 23 Nov 2021 15:02:59 -0800