Variable definitions

In Java, we define variables like this:

String str = "hello";
int num = 1;

In Scala, we define variables like this:

val str: String = "hello"
var num: Int = 1;

The difference between the two:

  • Java declares the type of the variable and then defines the name of the variable
  • Scala uses var to define the name of a variable and then appends its type
  • Scala can also not specify its type, and Scala will infer the type of the variable based on the assignment. So the following statement prints int and class java.lang.string.
  • Once a Scala variable type is defined, the type cannot be modified, so the statements we comment out below will not compile, just as in Java.

    var obj1 = 1
    println(obj1.getClass)
    //obj1 = "obj1"
    
    var obj2 = "obj2"
    println(obj2.getClass)
    //obj2 = 1

    val

    Final in Java and val in Scala are both used to modify immutable variables. If it is a value type, the value cannot be changed, and if it is an object type, the reference to the object cannot be changed. Final in Java, comment out statements will not compile:

    final int fNum = 1; // fNum=2; //Cannot assign a value to final ariable 'fNum'

    In Scala val, comment out statements cannot compile:

    val fNum = 1
    //fNum = 2

    Lazy loading

    Define a lazy load by lazy val

    lazy val lz = {
    println("lazy")
    "lz"
    }
    println("-----")
    println(lz)

    Println (“lazy”) is called only when lz is actually called. The result of the println(“lazy”) is shown below.

    -----
    lazy
    lz

Block expression

A block expression is a number of expressions wrapped in {}, the last of which is the return value of the block. In the following block expression, the value of x is 1.

var x = {
  println("block")
  1
}
println(x)

Definition of a function

Functions are defined with the arguments passed in on the left, the block expression on the right (omit {} if there is only one sentence), and the middle symbol =>. Let’s say we define an anonymous function:

(x: Int) => x + 1

Give the function a name:

Var fun1 = (x:Int)=> x+1 println(fun1(10)

No arguments function:

var fun2=()=>10
println(fun2())

Method definition

The definition of a method is similar to the definition of a function. First, we define the keyword def of the method, then the type of the parameter, the type of the return value, =, and finally the body of the method.

def add(x: Int, y: Int): Int = x + y

The parameter type can be left unwritten if it is no arguments. In addition, the return value can not be written, the compiler will automatically infer.

Def method1()={1} println(method1())// Print 1

If = is not written, then the return value of this method is Unit, which is equivalent to Java void.

Def method2(){1} println(method2()))

The data type

  1. Any is the supertype for all types, also known as the top type. It defines some common methods, such as Equals, HashCode, and ToString. Any has two immediate subclasses: anyVal and anyRef.
  2. AnyVal is the parent of all value types, including Double, Float, Long, Int, Short, Byte, Char, Boolean, and Unit. The first few wrapper classes that are similar to Java’s primitive types, Unit is similar to Java’s void and is also a subclass of anyVal in Scala.
  3. AnyRef is the parent of all reference types, similar to Java’s Object.
  4. NULL is a subclass of all reference types, similar to NULL in Java.
  5. Nothing is a subclass of all types.