Scala Notes (1) - Basic Knowledge

The relationship between Scala and Java

  • 1. They are all based on the JVM virtual machine.

    The files compiled by Scala are also .classes, which must be converted into bytecodes and then run on the JVM virtual machine.

  • 2. Scala and Java call each other

    You can directly call Java code in Scala, and you can also directly call Scala code in Java

  • 3、Java8 VS Scala

    1) Before Java8 (lambda) came out, Java was only an object-oriented language, but after Java8 came out, Java was a mixed language of object-oriented and function-oriented.

    2) First of all, we have to precisely locate Scala. To a certain extent, Scala is not a purely function-oriented programming language. Some people think that Scala is a static object-oriented language with closures), more precisely , Scala is a mix of function-oriented and object-oriented.

    3) The original intention of Scala's design was to be FP-oriented, while Java started out as object-oriented OO. Now both are mixed languages ​​of OO and FP. Can you think so: Scala = FP + OO, and Java = OO + FP?

    Since the two paradigms of object-oriented OO and function-oriented FP are similar to the abscissa and ordinate, they are thinking in different coordinate directions, similar to the mismatched impedance relationship between the database and the object. Will have the effect of 1+1>2.

    Object-oriented is the closest to the human way of thinking, and function-oriented is the closest to the computer way of thinking. If you want computers to serve people's business modeling, then focus on OO; if you want computers to automatically model from big data through algorithms, then focus on FP. Therefore, Java may still occupy the main market in enterprise engineering software, while Scala will seize the Java market in the fields of scientific computing and big data analysis. For example, Scala's Spark has a tendency to replace Java's Hadoop .

 

Scala Lazy Features

1. When val is declared lazy, its initialization will be deferred until we first evaluate it. E.g,

lazy val lines= scala.io.Source.fromFile("D:/test/scala/wordcount.txt").mkString

2. If the program never accesses lines, the file will not be opened. But intentionally misspelled the filename. No error is reported when the initialization statement is executed. However, once you access words, you will get an error message: file not found.

3. Lazy values ​​are useful for expensive initialization statements. They can also cope with other initialization problems, such as circular dependencies. More importantly, they are the basis for developing lazy data structures. (The bottom layer of spark relies heavily on these lazy)

4. Loading (it will only be loaded when you use it)

println(lines)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324861707&siteId=291194637