Learning Scala Chapter 2

1. Value and variable definitions

The definition of value

Value, that is, constant, immutable, basic syntax definition:

val <name>:<type> = <literal>
Copy the code

Create a value of type Int called a and assign it to 10

scala> val a:Int = 5
a: Int = 5
scala> a = a/4
:12: error: reassignment to val       
a = a/4         ^
Copy the code

Note: The immutability of a value makes it impossible to assign a changing value to yourself.

Variable definitions

Variables, which are mutable, are syntactically defined as:

var <identifier>:<type> = <data>
Copy the code

Create a variable b of type Double and assign the value 5.1

scala> var b:Double = 5.1
b: Double = 5.1

scala> b = 5.1 / 2
b: Double = 2.55
Copy the code

If the type is not specified, that is, there is no “:”, the Scala compiler deduces its type from the assigned data.

value

scala> val c = 5.3
c: Double = 5.3

scala> val c1 = 5
c1: Int = 5

scala> val c2 = "hello world!"
c2: String = hello world!
Copy the code

variable

scala> var c = 5.3
c: Double = 5.3

scala> var c1 = 5
c1: Int = 5

scala> var c2 = "hello world!"
c2: String = hello world!
Copy the code

Values are compared to variables

Values maintain global invariance, making data stable and predictable.

2, named

A letter followed by 0 or more letters and numbers, for example, word5344

A letter is followed by zero or more letters and numbers, followed by an underscore (_), followed by one or more letters and numbers, or one or more operation characters. Such as rwe34_tr $

One or more operation characters

3. Data types

Scala supports Byte, Short, Int, Long, Float, and Double data types

Supports upward conversions, such as Int to Long, but does not support Long to Int, because conversion space is limited, data loss is likely to occur.

scala> val a:Int = 5
a: Int = 5

scala> val b:Long = a
b: Long = 5

scala> val c:Long = 10
c: Long = 10

scala> val d:Int = c
:12: error: type mismatch;
 found   : Long
 required: Int
       val d:Int = c
Copy the code

But you can use the toType method to do the conversion.

scala> val c:Long = 10
c: Long = 10

scala> val d:Int = c.toInt
d: Int = 10
Copy the code

The conversion functions for different types of data are as follows:

ToString, toInt, toDouble, toFloat, toLong

String and interpolation

Use double quotation marks. When special characters need to be escaped by backslash

scala> val hello:String = "hello world"
hello: String = hello world

scala> val signature:String = "With Regards,\nYour friends"
signature: String =
With Regards.Your friends
Copy the code

Use single quotes for Char literals and double quotes for strings. && and | |, similar to or on the function, as long as the first parameter is enough to conclude that they will not have to calculate the second parameter; & and |, similar to, and will be in before returning to the results of two parameters are checked.

If the string is equal, use “==” to check whether the string is equal.

scala> val greeting:String = "hello world"
greeting: String = hello world

scala> val matched = (greeting == "hello world")
matched: Boolean = true
Copy the code

String interpolation, which can be used for parameter references.

To concatenate multiple strings, use “+”

scala> val approx:Float = 355 / 113f
approx: Float = 3.141593

scala> println("Pi,using 335/113f,is about " + approx + ".")
Pi,using 335/113f,is about 3.141593.
Copy the code

Inserts variables into a string

You need to prefix the string with an “s” and then use $to indicate references to external data. Curly braces are optional.

scala> val approx:Float = 355 / 113f
approx: Float = 3.141593

scala> println(s"Pi,using 355/113,is about $approx.")
Pi,using 355/113,is about 3.141593.
Copy the code

If the reference is indistinguishable from the surrounding text, curly braces are required

scala> val item:String = "apple"
item: String = apple

scala> s"How do you like them ${item}s?"
res4: String = How do you like them apples?

scala> s"Fish n chips n vinegar,${"pepper "*3}salt"
res5: String = Fish n chips n vinegar,pepper pepper pepper salt
Copy the code

Use printf to control data formatting, such as the number of characters and the display of small values.

How to use: Need to change prefix to “f”

scala> val item:String = "apple"
item: String = apple

scala> f"I wrote a new $item%.3s today"
res6: String = I wrote a new app today

scala> f"Enjoying this $item 335/113.0 ${}%.5f times today"
res9: String = Enjoying this apple 2.96460 times today
Copy the code

5, tuples

An ordered container containing two or more values, such as (5,”data”,”she”)

Create a tuple, or use the relational operator -> to represent key-value pairs

scala> val info:(Int.String.Boolean) = (5."Korben".true)
info: (Int.String.Boolean) = (5.Korben.true)

scala> val red = "red" -> "0xff0000"
red: (String.String) = (red,0xff0000)

scala> val reversed = red._2 -> red._1
reversed: (String.String) = (0xff0000,red)
Copy the code

Tuples index

scala> val name = info._2
name: String = Korben
Copy the code