1. Data types and operators

1.1 Data Types

The base type Type specification
Byte An 8-bit signed integer
Short 16 – bit signed integer
Int 32 – bit signed integer
Long 64 – bit signed integer
Char A 16-bit unsigned Unicode character
String Char sequence (string)
Float 32-BIT single-precision floating point number
Double 64 – bit double – precision floating point number
Boolean True or false

Notice the difference between Scala types and Java

[!NOTE]

  1. All types in Scala begin with a capital letter
  2. Use plasticIntInstead of an Integer
  3. Variables defined in Scala can be typed without being written, allowing the Scala compiler to infer automatically

1.2 the operator

category The operator
Arithmetic operator +, -, *, /
Relational operator >, <, ==! =, >=, <=
Logical operator , &&, | |,!
An operator &, | |, ^, < <, > >
  • Scala does not have the ++ and — operators

  • Unlike Java, in Scala, you can use ==,! Eq is used when comparing reference values

Reference code

val str1 = "abc"
val str2 = str1 + ""
str1 == str2   true
str1.eq(str2)   false
Copy the code

1.3 Scala Type Hierarchy

type instructions
Any All types of, which has two subclasses AnyRef and AnyVal
AnyVal All numeric typesThe parent class
AnyRef The parent class of all object types (reference types)
Unit Null, Unit is a subclass of AnyVal and has only one instance {% em %}() {% endem %}

It is similar to void in Java, but Scala is much more object-oriented than Java
Null Null is a subclass of AnyRef, which means it is a subclass of all reference types. Its instance is {% em %}null{% endem %}

Null can be assigned to any object type
Nothing All typesA subclass

You cannot create an instance of this type directly. When a method throws an exception, it returns Nothing, because Nothing is a subclass of all classes, so it can be assigned to any type

Conditional expressions

2.1 Has a return value

[!NOTE]

  • In Scala, conditional expressions also return values
  • In Scala, there are no ternary expressions and you can use if expressions instead
scala> val age:Int = 20
age: Int = 20
// The result expression can return the result
scala> val result:Boolean = if(age == 20) true else false
result: Boolean = true

scala> print(result)
true
Copy the code

2.2 Block expressions

  • In Scala, {} is used to represent a block expression
  • Like if expressions, block expressions have values
  • The value is the value of the last expression
// return null because there is no last expression
scala> val a = {
     | println("jack")
     | }
jack
a: Unit = ()

// return the last value of 1 + 1
scala> val b = {
     | println("haode")
     | 1 + 1
     | }
haode
b: Int = 2
Copy the code

3. The cycle

3.1 For Expression

grammar

for(I <- expression/array/set) {/ / expression
}
Copy the code

3.1.1 Simple Loop

Walk through the numbers 1 to 10

// we can get the numbers from 1 to 10 first, and then iterate
scala> val nums = 1.to(10)                                                              
nums: scala.collection.immutable.Range.Inclusive = Range(1.2.3.4.5.6.7.8.9.10) 
                                                                                        
scala> for(i <- nums) println(i)      

// 2, use infix call method (later chapter will talk about, here is not introduced to everyone)
scala> for(i <- 1 to 10) println(i)
Copy the code

3.1.2 Nested loops

for(i <- 1 to 3; j <- 1 to 5) {print("*");if(j == 5) println(""} result: ***** ***** *****Copy the code

3.1.3 guard

A guard is the ability to add an if statement to a for expression

grammar

for(I <- expression/array/setifExpression) {/ / expression
}
Copy the code
// Add a guard to print a number divisible by 3
for(i <- 1 to 10 if i % 3= =0) println(i)
Copy the code

3.1.4 Derivation for

  • In the future, you can use the for derivation to generate a new set (a set of data)

  • In the body of a for loop, we can use yield expressions to build a collection. We call for-expressions that use yield expressions derivations

// For: the for expression starts with yield and builds a collection
scala> val a = for(i <- 1 to 10) yield i * 10
a: scala.collection.immutable.IndexedSeq[Int] = Vector(10.20.30.40.50.60.70.80.90.100)
Copy the code

3.2 the while loop

A while loop in Scala is the same as a while loop in Java

scala> var i = 1
i: Int = 1

scala> while(i <= 10) {
     | println(i)
     | i = i+1|}Copy the code

3.3 break and continue

  • In scala, the break/continue keyword similar to Java and C++ has been removed
  • If you must use break/continue, you need to use the breable and break methods of the break class of the Scala.util. control package.

3.3.1 implementation break

// import Break in scala.util. Control
import scala.util.control.Breaks. _// 2. Wrap the for expression with breakable
breakable{
    for(i <- 1 to 100) {
// 3. Add the 'break()' method call where the for expression needs to exit the loop
        if(i >= 50) break(a)else println(i)
    }
}
Copy the code

3.3.2 rainfall distribution on 10-12 realize the continue

// import Break in scala.util. Control
import scala.util.control.Breaks. _// 2. Wrap the body of the for loop with breakable
for(i <- 1 to 100 ) {
    breakable{
        if(i % 10= =0) break(a)else println(i)
    }
}
Copy the code