158 Scala Basic Grammar

Keywords: Scala Java Programming

1. Declare variables

package cn.itcast.scala

object VariableDemo {
 
  def main(args: Array[String]) {
    //Variable values defined with val are immutable, equivalent to variables modified with final in java
    val i = 1
 
    //Variables defined with var are changeable, and val is encouraged in Scala
    var s = "hello"
 
    //The Scala compiler automatically infers the type of the variable and specifies the type if necessary.
    //Variable name comes first, type comes last
    val str: String = "itcast"
  }
}

2. Common types

Scala, like Java, has seven numerical types Byte, Char, Short, Int, Long, Float and Double (unpackaged type) and a Boolean type.

3. Conditional expressions

Scala's conditional expressions are concise, such as:

package cn.itcast.scala


object ConditionDemo {
  def main(args: Array[String]) {
    val x = 1
    //Judge the value of x and assign the result to y
    val y = if (x > 0) 1 else -1
    
    //Print the value of y
    println(y)

    //Supporting mixed type expressions
    val z = if (x > 1) 1 else "error"
    
    //Print the value of z
    println(z)

    //If else is missing, it is equivalent to if (x > 2) 1 else ()
    val m = if (x > 2) 1
    println(m)

    //Every expression in scala has a value, and scala has a Unit class, written as (), which is equivalent to void in Java.
    val n = if (x > 2) 1 else ()
    println(n)

    //If and else if
    val k = if (x < 0) 0
    else if (x >= 1) 1 else -1
    println(k)
  }
}

4. Block expressions

package cn.itcast.scala

object BlockExpressionDemo {
  def main(args: Array[String]) {
    val x = 0
    //In scala, {} class contains a series of expressions. The value of the last expression in the block is the value of the block.
    //Here's a block expression
    val result = {
      if (x < 0){
        -1
      } else if(x >= 1) {
        1
      } else {
        "error"
      }
    }
    //The value of result is the result of a block expression.
    println(result)
  }
}

5. cycle

There are for loops and while loops in scala, and for loops are more common.
For loop grammar structure: for (i<-expression/array/set).

package cn.itcast.scala

object ForDemo {
  def main(args: Array[String]) {
    //For (i < - expression), expression 1 to 10 returns a Range (interval)
    //Each cycle assigns a value in the interval to i
    for (i <- 1 to 10)
      println(i)

    //For (i < - arrays)
    val arr = Array("a", "b", "c")
    for (i <- arr)
      println(i)

    //Advanced for loop
    //Each generator can take a condition, note that there is no semicolon before if
    for(i <- 1 to 3; j <- 1 to 3 if i != j)
      print((10 * i + j) + " ")
    println()

    //For derivation: If the loop body of a for loop starts with yield, the loop builds a set
    //Each iteration generates a value in the set
    val v = for (i <- 1 to 10) yield i * 10
    println(v)

  }

}

6. Calling methods and functions

Operators such as + - */% in Scala work as well as Java, and bitwise operators & | ^ >> *.
It's just a little special: these operators are actually methods.

For example: a + b
This is an abbreviation of the following method calls:
a.+(b)

Method B can be written as a. Method (b)

7. Definition of methods and functions

1. Definition method

The return value type of the method can not be written, and the compiler can deduce it automatically, but for recursive functions, the return type must be specified.

2. Defining Functions

3. Differences between methods and functions
In functional programming languages, functions are "first-class citizens" that can be passed and manipulated like any other data type.

Case: First define a method, then define a function, and then pass the function into the method.

package cn.itcast.scala

 
object MethodAndFunctionDemo {
  //Define a method
  //Method m2 parameter requires a function whose parameters must be of two Int types
  //The return value type is also Int type
  def m1(f: (Int, Int) => Int) : Int = {
    f(2, 6)
  }

  //Define a function f1, parameter is two Int types, return value is an Int type
  val f1 = (x: Int, y: Int) => x + y
  //Define a function f2 again
  val f2 = (m: Int, n: Int) => m * n

  //main method
  def main(args: Array[String]) {

    //Call the m1 method and pass in the f1 function
    val r1 = m1(f1)
    println(r1)

    //Call the m1 method and pass in the f2 function
    val r2 = m1(f2)
    println(r2)
  }
}

4. Convert methods into functions (magical underscores)

Posted by pure_skill_2000 on Sun, 06 Oct 2019 09:45:47 -0700