Introduction to Scala

[TOC]

What is Scala?

  • Formally speaking, Scala is a multi-paradigm programming language that combines the features of object-oriented programming with functional programming. Scala, like Java, runs on a Java virtual machine and generates class bytecode files after compilation, so Scala also has the cross-platform feature of being able to write once and run anywhere.
  • To put it more colloquially, Scala is a powerful language with elegance, simplicity, speed, and so on. For those of us who live in the world of apes, what better way to appeal to us than to act like a force? As you dig deeper, many of the regrets in Java are well implemented in Scala. Spark’s underlying implementation is in Scala, and if you want to learn more about the big data framework, learn the language.

The installation of the Scala

As mentioned earlier, Scala runs on the JVM, so in order to run our program, we first need to make sure that we have the JDK installed on our computer, which is available directly on oracle’s website. The latest version is JDK9.

Once the runtime environment is installed, the next step is to install Scala’s compiler, which compiles Scala source into class bytecode files and runs them in the JVM as if they were Java bytecode files.

  • Install the Scala compiler on Windows

    Go to scala’s official website and download the Windows installation package

    image.png

  • Install Scala in Linux

    Download the scala-sources-2.12.4.tar.gz file in the screenshot above and unzip it to your favorite directory in Linux

    Tar -zxvf scala-2.10.6. TGZ -c /usr/share/localCopy the code

    Configure the environment variables and edit the /etc/profile file with the Vim editor

    export JAVA_HOME=/usr/share/local/jdk
    export PATH=$PATH:$JAVA_HOME/bin:/usr/share/local/scala/binCopy the code

    After editing and saving, don’t forget source to make the environment variable take effect immediately

    source  /etc/profileCopy the code

Let’s test it out with HelloWorld

  • According to international conventions, learning any language begins with HelloWorld, just like the ribbon-cutting ceremony for the opening of a new store. Let’s start with a new hello. scala file. As follows:

    object Hello{
        def main(args: Array[String]) {
            println("Hello World!")}}Copy the code

    This is similar to the Java main function, but the syntax looks a little different, careful friends may notice that there is no end to each line of code; End with a semicolon. This is the principle of Minimalism in Scala.

    Compile source file:

    scalac ./Hello.scalaCopy the code

    The hello. class file is generated in this directory, and the next class is run:

    scala HelloCopy the code

    image.png

    If the following result is displayed, the operation is successful. Let’s open the door to Scala’s alien world.

  • Class files can be decompiled into Java code. Can class files generated by Scala be decompiled into Java files? Of course you can:

    import scala.reflect.ScalaSignature;
    public final class Hello
    {
      public static void main(String[] paramArrayOfString)
      {
        Hello..MODULE$.main(paramArrayOfString);
      }
    }Copy the code

    This is the Decompiler result, so this is fine. In fact, Scala and Java are almost seamless. In Scala you can refer directly to Objects in Java, so you can use Java’s rich third-party frameworks in Scala, which we’ll cover later.

Scala development tools

  • To do a good job, he must sharpen his tools. If you want to develop a large project, you can’t always write code in the text editor. You usually develop in IDEA. After installing IDEA, you can install a Scala plug-in.

    image.png

    The name of the plug-in is Scala, so just search for it and install it.

    Create HelloWorld as before:

    image.png

conclusion

Ok, that’s about it. More basics and advanced uses of Scala will be covered later. Interested partners can follow my wechat public account, I will update as soon as possible.

img