Scala programming basic grammar (1)

The relationship between Scala and Java

Because Scala is based on the Java Virtual Machine, a programming language for the JVM. All Scala code needs to be compiled into bytecode and then run by the Java virtual machine.

So Scala and Java can interoperate seamlessly. Scala can call Java code arbitrarily. So the relationship between Scala and Java is very, very close.

Install Scala

Download from the Scala official website, http://www.scala-lang.org/download/ , the installation package for the windows version is scala-2.11.7.msi.
· Install Scala using the downloaded installation package.
· In the PATH environment variable, configure the $SCALA_HOME/bin directory.
·In the windows command line, you can directly type scala, open the scala command line, and perform scala programming.

Using the Scala Interpreter

  • REPL: Read -> Evaluation -> Print ->
    Loop. The scala interpreter, also known as the REPL, quickly compiles scala code into bytecode, which is then handed over to the JVM for execution.

  • Evaluate expressions: In the scala> command line, type scala code, and the interpreter will return the result directly to you. If you don't specify a variable to store the value, the value defaults to res and displays the data type of the result, such as Int, Double, String, etc.
    For example, enter 1 + 1 and you will see res0: Int = 2

  • Built-in variable: You can continue to use the res variable and the value it stores later.
    For example, 2.0 * res0, return res1: Double = 4.0
    For example, "Hi, " + res0, return res2: String = Hi, 2
  • Autocompletion: In the scala>command line, you can use the Tab key for autocompletion.
    · For example, enter res2.to, hit the Tab key, the interpreter will display the following options, toCharArray, toLowerCase, toString, toUpperCase. Because it is impossible to determine which one you need to complete at this time, all options will be provided to you.
    · For example, enter res2.toU, hit the Tab key, and it will directly complete res2.toUpperCase for you.

declare variable

  • Declare the val variable: You can declare the val variable to store the result of the evaluation of the expression.
    · For example, val result = 1 + 1
    · These constants can continue to be used, for example, 2 * result
    · But after the constant is declared, its value cannot be changed, for example, result = 1, it will return error: reassignment to val's error message.
  • Declaring a var variable: If you want to declare a reference whose value can change, you can use a var variable.
    For example, val myresult = 1, myresult = 2
    However, in scala programs, it is usually recommended to use val, which is constant, so for example, in large and complex systems like spark, a large amount of network transmission data is required. If var is used, it may be Worry about values ​​being changed by mistake.
    · Similar features are used in the design and development of large and complex systems in Java. We usually design objects passed to other modules/components/services as Immutable Classes. It will also use java constant definitions, such as final, to prevent the value of the variable from being changed. Thereby improving the robustness and security of the system.
  • Specify the type: Whether declaring a val variable or declaring a var variable, you can manually specify its type. If you don't specify it, scala will automatically infer the type based on the value.
    For example, val name: String = null
    For example, val name: Any = "leo"
  • Declare multiple variables: You can declare multiple variables together.
    For example, val name1, name2:String = null
    For example, val num1, num2 = 100

data types and operators

  • Basic data types: Byte, Char, Short, Int, Long, Float, Double, Boolean.
    At first glance, it is the same as the wrapper type of Java's basic data type, but scala has no concept of basic data type and wrapper type, and they are all classes. Scala itself will be responsible for the conversion of basic data types and reference types.
    · Using the above types, you can directly call a large number of functions, for example, 1.toString(), 1.to(10).
  • Enhanced types of types: Scala uses many enhanced classes to add hundreds of enhanced functions or functions to data types.
    · For example, the String class is augmented with a number of functions by the StringOps class, "Hello".intersect("World").
    For example, Scala also provides RichInt, RichDouble, RichChar and other types, and RichInt provides the to function, 1.to(10), where Int is implicitly converted to RichInt, and then its to function is called
  • Basic operators: Scala's arithmetic operators are no different from Java's arithmetic operators, such as +, -, *, /, %, etc., as well as &, |, ^, >>, <<, etc.
    However, in scala, these operators are actually functions of data types, such as 1 + 1, which can be written as 1.+(1)
    . For example, 1.to(10) can be written as 1 to 10.
    scala There is no ++, – operator provided in , we can only use + and -, such as counter = 1, counter++ is wrong and must be written as counter += 1.

Function call and apply() function

  • Function calling method: In scala, function calling is also very simple.
    · For example, import scala.math._, sqrt(2), pow(2, 4), min(3, Pi).
    The difference is that if you don't need to pass parameters when calling a function, scala allows you to omit parentheses when calling a function, for example, "Hello World".distinct

  • apply function
    · The apply function in Scala is a very special function. In a Scala object, the apply function can be declared. Using the form of "class name ()" is actually an abbreviation of "class name.apply()". Objects of a class are usually constructed this way, rather than the "new classname()" approach.
    For example, "Hello World" (6), because there is a function definition of def apply(n: Int): Char in the StringOps class, so "Hello World" (6) is actually "Hello World".apply(6 )abbreviation of.
    · For example, Array(1, 2, 3, 4) actually uses the apply() function of the Array object to create an instance of the Array class, which is an array.

Guess you like

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