A systematic study of Scala has recently begun. In fact, I used Scala before, for example, when I was using Gatling's performance testing tool, I was exposed to Scala. Gatling itself is written in Scala, and the Gatling performance Test profile itself is a Scala class that is free to use Scala and even the various class libraries provided by Java. At the time, the reason why I felt particularly comfortable with Gatling was the powerful expressiveness of the configuration file. And this expressiveness is provided by the Scala language.
Still, learning Scala starts with the simplest Hello world. The notable headline in the Scala website is:
Object-oriented Meets functional
The best of both worlds. Construct Elegant class hierarchies for maximum code reuse and extensibility, implement their behavior using Higher-order Functions. Or anything in-between.
It can be seen that Scala combines the two programming paradigms of object-oriented and functional programming. In this article I will compare the Java language with it to see exactly where Scala is strong.
The code for implementing a Hello World using the Java language is as follows:
1 2 3 4 5 |
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}
|
The following are the Scala implementations:
1 2 3 4 5 |
object HelloWorld { def main(args: Array[String]): Unit = { println("Hello, World!") }}
|
Well, if the punctuation is not the line of code, the Java version and the Scala version of the implementation of the code line number is consistent. Scala doesn't seem to be reducing anything. The only reduction is that println does not have to specify the class name, and the main method does not have to be declared static.
Here is a description of some of the differences between the Scala implementation and the Java implementation.
The first is that Scala uses object for HelloWorld modifiers. In fact, there are class keywords in Scala, so what's the difference between the object keyword and the class keyword? Simply put, the Object keyword defines an anonymous class and creates a single instance of the anonymous class (in singleton mode) with the name HelloWorld. So the methods defined in object are automatically static. I think one of the value of the Object keyword exists is the establishment of object-oriented and functional bridge. Because in an object-oriented system, all methods must exist in a class, and functional programming does not have the concept of a class, the use of a function does not require an instance of the new class, so the methods in object are static methods that can be called directly. Further interpretation please come here.
The second is that the types of variables in Scala are defined in the same way that the variable name is preceded by the type, separated by a colon. One reason is that the code is more readable. Because we care more about variable names, and type second, especially if you have a super-long type (such as Hashmap<shape, Pair<string, string>>) The second reason is said to be this way it is technically easier to implement Scala types. Further interpretation please come here.
The third is that the return value of the main function is unit, not void in Java. Why is that so? I think it's because Scala. In order to implement its own type system, it is inappropriate to use void directly for functions that do not have an explicit return value. In the Scala.unit document, this defines the unit:
Unit is a subtype of Scala. Anyval. There is only one value of type Unit, (), and it isn't represented by any object in the underlying runtime system. A method with return type Unit was analogous to a Java method which is declared void.
- The last semicolon of the expression in Scala is optional. The reason is to fit the philosophy of functional programming, which is as simple as possible. Do not write semicolons programmers can knock some code out one day.
Well, in Scala, Hello World has a way of writing.
1 2 3 |
object HelloWordV2 extends App { println("Hello, World!")}
|
The code was reduced from three lines to two lines, and Scala finally won. So, what's the app, a ghost? The app is a trait. What the hell is trait? Trait is a special type in Scala, similar to interface in Java, but more powerful than interface. HelloWordV2 adds an extension to the app and automatically becomes a running program, because the main method is defined in the app, so there's no need to define it in HelloWorldV2. The trick is that the code in the body of the HelloWorldV2 is executed as the code in the Main method. The following is the definition of the main method in the app source code:
1 2 3 4 5 6 7 8 9 Ten One A - - |
@deprecatedOverriding("main should not being overridden", "2.11.0" ) def main(args: Array[String ]) = {This . _args = args For (proc <- initcode) proc() if (util. Properties . Propisset ("Scala.time")) { val Total = currenttime - executionstart Console. println ("[Total" + total + "Ms]") }
|
It seems that the app has also set a timer when implementing the Main method, by scala.time this property to switch. So it's OK to turn over the source code is very fun.
I do not know whether the main method is a parameter called args, then the new version of HelloWorld How to use the parameter that? Just use it right on the line.
1 2 3 |
object HelloWordV2 extends App { println("Hello, World!" + args(0))}
|
The Ok,scala version of Hello World is here.
Learn the first of Scala-start with Hello World