Introduction to Scala basic syntax

1. Definition of variables

Note: there is no concept of static in scala. All methods and properties in the class decorated by the keyword object are static. The object class here is called companion class, which is not involved at present.


object Test01{
    def main(args: Array[String]): Unit = {
        //The variables defined with val are immutable, which is equivalent to the final decorated in java
        val v1=1
        //The value of variable defined by var is variable, and val is encouraged in scala
        var v2=2
        //When defining a variable, you can also provide the type before the variable name. How not to provide the default is type inference
        val v3:String ="zzy"  //Custom type
        val v4="jj"    //Type inference

2. Data types of Scala

scala, like java, has seven data types: byte, char, short, int, long, float, double, and boolean, but no wrapper type, and String.


object Test01 {
    def main(args: Array[String]): Unit = {
        val v1: Int = 1
        val v2: Double = 1.1
        val v3: Float = 1.1f
        val v4: Char = 'c'
        val v5: Boolean = true
        val v6: String = "zzy"
        val va7:String="""aaa \t\t0"""  //Three quotation marks indicate that special characters are not escaped
        val va8: Byte = 0

        //Note that if the data type of the variable is determined above, it cannot be modified below
        val value1=1
        value1="aaa" //Syntax errors will be reported here

As shown in the above figure:
Any: is the parent of all classes, including value type AnyVal and reference type AnyRef
AnyVal: is the parent class of all value types, including Int, Double, Boolean, Unit, etc
AnyRef: is the parent of all reference types, including Null
Null: it is a subclass of all reference types. There is only one instance of null. It is null, which is equivalent to null in java. It can be assigned to any reference type variable, but not to a value type variable
Nothing : Nothing
Unit: there is only one instance of the type, which is (), which is equivalent to void in java and has no real meaning (used when defining the return value of the method)

3. Basic operators of Scala

Arithmetic operator: + - * /%
Relational operator: > = < = ==
Logical operation: & & |!
Bit operation: & ^ > >
Object comparison: 1 = = 1 "zy" = = "zy" (equal judgment value)

4. Key words

5. Control flow statement

  (1) selection structure

object Test01 {
    def main(args: Array[String]): Unit = {
        val v1=5
        //Judgment statement with return value (if else)
        val result1=if(v1>5)"true" else "false"
        //Judgment statement with return value of () (if else if - else)
        val result2=if(v1>3) "true" else if(v1>5)  "false" else ()

  (2) block expression

object Test01 {
    def main(args: Array[String]): Unit = {
          * The value of the last expression in the block is the fast return value.
          *  Even if it is an assignment expression, it also has a return value, which is null, Unit.
        val x=5
        val y=10

        val result1={
        val result2={

  (3) loop statement

object Test01 {
    def main(args: Array[String]): Unit = {
        //for cycle
        //1 to 10
        for(i<- 1 to 10){
            println(i) //Print 1 to 10
        //1 until 10
        for(i<- 1 until 10){
            println(i) //Print 1 to 9
        //Iteratable object traversal
        val arrs=Array("aa","bb","cc")
        for(item <- arrs){
        //In traversal, each element is manipulated
        var array:Array[Int]=Array(1,2,3,4)
        //Every element in the array * 5
        val collect=for(i<- array) yield i*5
//Note: to and until can also have a second parameter, for example, 1 to (10,3) the second parameter represents the step size


var i=1
            //i + + / / note that these two sentences are wrong. There is no + + in scala--

Tags: Programming Scala Java

Posted on Tue, 03 Dec 2019 17:01:26 -0500 by karpagam