“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”

Know Scala

What is Scala?

In a nutshell, Scala is a high-level programming language that combines object-oriented and functional programming paradigms. Scala’s static typing system effectively reduces bugs in complex applications (building robust systems). Scala supports both JVM and JavaScript environments. This feature will make it easier to build high-performance systems based on the existing large library ecosystem.

Scala Features

  • Scala was created under the influence of Java, Ruby, Smalltalk, ML, Haskell, Erlang, and other languages

  • In statically typed languages, where the type of a variable is determined at compile time, Scala relies on its own type inference system

  • Has a complex type deduction system. By using type deduction, code written in Scala can be as compact as code written in dynamically typed languages.

  • Concise syntax, but still readable, and with Scala, verbose expressions in Java are gone, replaced by a concise Scala dialect.

  • Scala fully supports object-oriented programming (OOP). Scala improves Java’s object model by introducing traits that make it easy to implement new types through the use of mixin composition. In Scala, everything is an object, even a numeric type.

  • Scala fully supports functional programming (FP), which has come to be seen as the best tool for concurrency, big data, and code correctness. Using immutable values, functions that are treated as first-class citizens, functions that have no side effects, higher-order functions, and collections of functions helps you write clean, powerful, and correct code.

  • Scala source code can be compiled into.class files that run on the JVM.

  • Scala will make your programming work productive, but it’s also a deep programming language, and as you continue to program in Scala, you’ll find better ways to code. Some say Scala changes your programming mindset (and that’s a good thing).

Comments

// Single-line comment
Copy the code
/* Multiline comment */
Copy the code
/** ** multiline comment */
Copy the code

Naming conventions

Scala follows the camel name convention.

  • Class names: Person, StoreEmployee
  • Variable names: name, firstName
  • Method names: convertToInt, toUpper

Hello, World

object Hello {
  def main(args: Array[String) :Unit = {
    println("Hello World!")}}Copy the code
  • Create a file called hello. scala, write the code above and save it

  • Using the scalac command to compile the source files, we get the following two files

    • Hello$.class
    • Hello.class
$ scalac Hello.scala
Copy the code
  • usescalaThe command runs the bytecode file produced in the previous step
$ scala Hello
Copy the code

Tip:

  • objectwithclassSimilarly, you can use this when you only want an instance of this classobject
    • When compared to Java,objectIn themainMethods are similar to static methods of Java classes

Hello, World — Version 2

object Hello2 extends App {
  println("Hello World!")}Copy the code

The same result can be printed in the same way as the previous Section

Tip:

  • AppIs atraitThis Scala feature,AppContains themainMethod, so you don’t have to implement it yourself.traitSimilar to abstract classes in Java.