2.Scala- basis

  Chapter 2 Scala foundation

2.1 declared values ​​and variables

  Scala declare a variable in two ways, with a val, with a var.
  val / var variable name [: Variable Type] = variable value
  Val value definition is immutable, similar to a constant.
  
  Scala REPL default assignment val anonymous variable.

 

  Var variable declaration is variable.

Note: var and val keyword references are only identity itself can point to a different object, they do not
It indicates whether the object variable to which it refers. In order to reduce the variability caused by the bug, it should be possible to make
With immutable variables. Variable type can be omitted, the parser will be inferred value. val and var sound
Ming variable must be initialized.

 

2.2 common type

Scala has eight data types: Byte, Char, Short, Int, Long, Float, Double
And Boolean.
   Boolean: true or false
   Byte: 8 bits signed
   Short: 16 bit signed
   Int: 32 bit signed
   Long: 64 bit signed
   Char: 16 bits unsigned
   Float: 32 bit single precision floating point
   Double: 64 bit double precision floating point
 
  With different data types in java, Scala does not distinguish between primitive types and reference types, so this
These are the type of object, you can call the corresponding method. String is used directly
java.lang.String. However, because String is a collection of immutable series of Char,
Most of Scala for the set of operations, can be used for String, specifically, String of this
些方法存在于类 scala.collection.immutable.StringOps 中。 由于 String 在需要
时能隐式转换为 StringOps,因此不需要任何额外的转换,String 就可以使用
这些方法。
 
  每一种数据类型都有对应的 Rich* 类型,如 RichInt、RichChar 等,为基
本类型提供了更多的有用操作。

 

  由定义在 Predef 中的隐式转换完成将相应类型转换成 Rich*,然后调用
Rich*的相对应方法。【隐式转换后面会讲】
  在 Scala 中通过方法进行类型转换,不像 Java 的强制类型转换。
注:Scala 中没有强制转化,需要通过方法进行类型的转化。
 
  Scala 中,所有的值都是类对象,而所有的类,包括值类型,都最终继承
自一个统一的根类型 Any。统一类型,是 Scala 的又一大特点。更特别的是,
Scala 中还定义了几个底层类(Bottom Class),比如 Null 和 Nothing。
 
 
  1) Null 是所有引用类型的子类型,而 Nothing 是所有类型的子类型。Null 类
    只有一个实例对象,null,类似于 Java 中的 null 引用。null 可以赋值给任
意引用类型,但是不能赋值给值类型。
  2) Nothing,可以作为没有正常返回值的方法的返回类型,非常直观的告诉你
    这个方法不会正常返回,而且由于 Nothing 是其他任意类型的子类,他还
能跟要求返回值的方法兼容。
  3) Unit 类型用来标识过程,也就是没有明确返回值的函数。 由此可见,Unit
    类似于 Java 里的 void。Unit 只有一个实例,(),这个实例也没有实质的意
义。  

 

 

 

2.3 算术和操作符重载

  +-*/%可以完成和 Java 中相同的工作,但是有一点区别,他们都是方
法。你几乎可以用任何符号来为方法命名。
 
    a.方法(b) 等于 a 方法 b
注:Scala 中没有++、--操作符,需要通过+=、-=。

 

2.4 调用函数和方法

除了方法,Scala 还提供了函数,比如数学函数。
 
调用函数:
需要引入函数包 import 包名._  _是通配符,等同于 java 中的*

 

 
调用静态方法:
Scala 中没有静态方法,一般通过单例对象或者伴生对象进行实现。

 
调用对象方法:
对象实例调用方法

 

 

 

2.5 apply、update 方法

apply 方法是调用时可以省略方法名的方法。用于构造和获取元素
    “Hello”(4) 等同于 “Hello”.apply(4)
    Array(1,2,3) 等同于 Array.apply(1,2,3)

 

在 StringOps 中你会发现一个 def apply(n: Int): Char 方法定义。
update 方法也是调用时可以省略方法名的方法,用于元素的更新,
     arr(4) = 5 等同于 arr.update(4,5) 

 

 

 

 

2.6 option 类型

  Scala 为单个值提供了对象的包装器,表示为那种可能存在也可能不存在
的值。他只有两个有效的子类对象,一个是 Some,表示某个值,另外一个是
None,表示为空,通过 Option 的使用,避免了使用 null、空字符串等方式来
表示缺少某个值的做法。 

 

scala> val map1 = Map("name" -> "Lion", "age" -> "22")
map1: scala.collection.immutable.Map[String,String] = Map(name -> Lion, age -> 22)

scala> map1.get("name")
res0: Option[String] = Some(Lion)

scala> println(res0.get)
Lion

scala> map1.get("sex")
res2: Option[String] = None

scala> map1("name")
res3: String = Lion

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/LXL616/p/11105169.html
Recommended