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 these two:

  • Java is to declare the type of the variable first, and then define the name of the variable
  • Scala defines the name of the variable through var, and then adds his type after the variable
  • Scala can also not specify his type, then Scala will infer the type of the variable based on the assignment. So the following statement will print int and class java.lang.String.
  • After the Scala variable type is defined, the type cannot be modified, so the statement we commented out below will not compile. This is the same as java.
var obj1 = 1
println(obj1.getClass)
//obj1 = "obj1"

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

val

Java's final and Scala's val are both used to modify immutable variables. If it is a value type, the value cannot be changed. If it is an object type, then the reference of the object cannot be changed.
Java's final, commented out statements will not compile:

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

Scala's val, the commented out statement will not compile:

val fNum = 1
//fNum = 2

Lazy loading

Define a lazy loading through lazy val

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

The print result is as follows, which means that at the time of definition, there is no printing method. Only when lz is actually called, println("lazy") will be called.

-----
lazy
lz

Block expression

Block expressions are multiple expressions wrapped in {}, and the last expression is the return value of this block.
In the block expression below, the value of x is 1.

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

Function definition

When the function is defined, the left side is the incoming parameter, the right side is the block expression ( {} can be omitted if there is only one sentence), and the symbol in the middle is => .
For example, we define an anonymous function:

(x: Int) => x + 1

Give the function a name:

var fun1 = (x:Int)=> x+1
println(fun1(10))//输出11

No parameter function:

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

Method definition

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

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

If there is no parameter, the parameter type can be omitted. In addition, the return value does not need to be written, and the compiler will automatically infer it.

def method1()={
 1
}

println(method1())// 输出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())// 输出()

type of data

  1. Any is the super type of all types, also known as the top type. It defines some common methods, such as equals, hashCode, and toString. Any has two direct subclasses: AnyVal and AnyRef.
  2. AnyVal is the parent class of all value types, including Double, Float, Long, Int, Short, Byte, Char, Boolean, Unit. The first few basic types of wrapper classes similar to Java, Unit is similar to Java's void, which is also a subclass of AnyVal in Scala.
  3. AnyRef is the parent class 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.


大军
847 声望183 粉丝

学而不思则罔,思而不学则殆


引用和评论

1 篇内容引用
0 条评论