Scala Study Notes - Getting Started (a)

Scala Study Notes - Getting Started (a)


1.Scala interpreter

$Scala 
scala> 1+2
res0: Int = 3

Interpreter include:

  • An automatically generated or user-defined variable name, point value is calculated (res0: result 0)
  • A colon and colon expression result type (Int)
  • An equal sign
  • It is worth to by the expression evaluation results (3)

2. Define the variables

scala> val msg = "Hello world!"
msg : String = Hello world!

scala> val msg2: java.lang.String = "Hello again, world!"
msg2: String = Hello again, world!

scala> var greeting = "Hello, world!"
greeting: String = Hello, world!

Scala two types of variables: val (final analog variable), and var (non-final analog variable).
Annex: interpreter can use '|' input lines of code


3. Defined Functions

scala> def max(x:Int, y:Int):Int ={
    if(x > y) x
    else y
}
max:(x:Int, y:Int)Int

Function definition begins with def, followed by the function name in parentheses and comma-separated list of parameters. Behind each parameter must add type annotations to the beginning of the colon


4. Write a script Scala

Command line parameters can be obtained through an array named args of Scala. Scala array index starts from 0, parentheses can be accessed by the subject specified index corresponding to the element.
Scala compiler will ignore characters between // and the next newline character, and the characters / * and * / and.


The traversal and for using foreach

args.foreach(arg => println(arg))

In this code, the implementation of args foreach method, passing in a function. Function by passing a literal, anonymous function receives a parameter, called arg function body of the println (arg)

Function literals:
parameter parentheses with a group name, and a right arrow function body.

Guess you like

Origin www.cnblogs.com/ganshuoos/p/11942486.html