1.scala is the difference between matching and switch in java :
- switch can only match values, pattern matching supports more, can be any type ( List , Map , Array , types, etc. are supported)
- If there is no case match, an exception will be thrown, and swicth will exit directly
- Both case _ and defalut match other default cases
- There is no need to display break at the end of each case in pattern matching, it will not enter the next branch like swicth
- match is an expression and can be assigned to a variable, switch is a statement and cannot be assigned to a variable
- case statement supports guards (any boolean condition)
- The case keyword can be followed by a variable (the matched expression will be copied to this variable)
2. The case can be followed by a variable or a constant
- Constants: start with an uppercase letter or ` start with a lowercase letter`
- Variables: start with a lowercase letter
3.case matching type, jvm type erasure mechanism
- The Array type is kept intact and can match a specific Array [ T ]
- List , Map , Set , the type will be erased and cannot match a specific type T
Test code:
package demo.scala import java.io.File import scala.util.Random object App5 { final valueA = 'a' def main(args: Array[String]): Unit = { val array = Array("a", "b", "c") val name = array(Random.nextInt(array.length)) // matchType('a') //case _ // matchType(Set(1, 2)) //case f // matchType(Set("1")) //case f // matchType(List(1, 2)) //case c // matchType(Array("a", "b")) // matchArray(Array("b1")) // matchArray(Array("A")) // matchArray(Array("a1", "a2")) // matchArray(Array("a1", "a2", "a3", "a4")) // matchArray(Array("b1", "a2", "a3", "a4", "a5")) // matchList(List("A")) // matchList(List("B1", "B2")) // matchList(List("C")) // matchList(List("C", "C2", "C3")) // matchType(1) // matchType("abc") // matchType(List("a", "b")) // matchType(List(1, 2)) // matchType(Map("k1" -> 1, "k2" -> 2)) // matchType(Map("k" -> "v")) // testSwitch('a') // testSwitch(File.pathSeparatorChar) // matchTuple (Tuple2 (1, 0)) funcB() } def funcA(name: String, grade: String): Unit = { grade match { case "A" => println("A") case "B" => println("B") case _ if name == "lisi" => println("lisi") case _ => println("C") } } def funcB() = { val test = "10" var arr = new Array[Object](2) arr(0) = "10" arr(1) = "12" val Array(`test`, x) = arr println(Array(`test`, x).map(println(_))) val E = 10 var array = Array(E, 11) var Array(E, y) = array println(Array(E, y)(0)) } def matchArray(array: Array[String]) = { array match { case Array("A") => println("A") case Array(a, b) => println(a + "," + b) case Array("a1", _*) => println("C") case Array("b1", rest@_*) => { if (rest.length == 0) println("空") } case _ => println("_") } } def matchList(list: List[String]) = { list match { case "A" :: Nil => println("A") //list with only one element A case x :: y :: Nil => println(list(0) + "," + list(1)) //list with two elements, (x is the first element, y is the second! !!) case "C" :: tail => println("C") //C is the first element of the list } } def matchType(objet: Any) = { //scala Chinese match is an expression, not a statement, it can be assigned to a variable, such as the following rs variable var rs = objet match { case Char => println("char") case a: Int => println("int type") //return (), because it is unit type case b: String => "string type" //return string case c: List[String] => println("list[String]") case d: Map[String, Int] => println("map[String,Int]") case e: Map[_, _] => println("map[_,_]") case f: Set[String] => println("f: set") case g: Array[Int] => println("g:Array[Int]") case _ => println("other") } // println("rs = " + rs) } def testSwitch(str: Char): Unit = { str match { case _ => println("other") case `finalA` => println("case 2") case separatorChar => println("case 1") } } def matchTuple(pair: Tuple2[Int, Int]) = { pair match { case (_, 0) | (0, _) => println("ok") //When there is a branch, only underscore can be used, other variable names cannot be used // case (b, 0) | (0, b) => println("fail") // will report an error } } }