Scala comments and variables

Keywords: Java Scala

1, Notes

Scala annotations use exactly the same as Java. Annotation is a good programming habit that a programmer must have. First sort out your thoughts through comments, and then reflect them in code.

1.1 single line notes

package com.atguigu.bigdata.scala

object ScalaComment{
    def main(args: Array[String]): Unit = {
        // Single-Line Comments 
    }
}

1.2 multiline notes

package com.atguigu.bigdata.scala

object ScalaComment{
    def main(args: Array[String]): Unit = {
        /*
           multiline comment 
         */
    }
}

1.3 document notes

package com.atguigu.bigdata.scala
/**
  * doc notes
  */
object ScalaComment{
    def main(args: Array[String]): Unit = {
    }
}

2, Variable

Variables are easy-to-use placeholders used to reference computer memory addresses. Variables will occupy a certain memory space after they are created. Based on the data type of the variable, the operating system allocates memory and determines what will be stored in reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or letters in these variables.

2.1 syntax declaration

The type of the variable is declared after the variable name and before the equal sign.

object ScalaVariable {
    def main(args: Array[String]): Unit = {
        // var | val variable name: variable type = variable value
        // User name
        var username : String = "zhangsan"
        // User password
        val userpswd : String = "000000" 
    }
}

If the type of a variable can be inferred from the variable value, the type declaration can be omitted. The omission here is not undeclared, but automatically declared and compiled by the Scala compiler at compile time.

object ScalaVariable {
    def main(args: Array[String]): Unit = {
        // Because the variable value is a string, and because Scala is a statically typed language, even if the type is not declared
        // Scala can also correctly judge the type of variables at compile time, which reflects the simplicity of Scala language.
        var username = "zhangsan"
        val userpswd = "000000" 
    }
}

2.2 variable initialization

In Java syntax, variables can be initialized before use, but it is not allowed in Scala syntax. Initialization must be displayed.

object ScalaVariable {
    def main(args: Array[String]): Unit = {
        var username // Error
        val username = "zhangsan" // OK
    }
}

2.3 variable variables

Variables whose values can be changed are called variable variables, but the variable type cannot be changed. Variable variables in Scala are declared with the keyword var

object ScalaVariable {
    def main(args: Array[String]): Unit = {
        // User name
        var username : String = "zhangsan"
        username = "lisi" // OK
        username = true // Error
    }
}

2.4 immutable variables

A variable whose value cannot be changed once initialized is called an immutable variable. In Scala, immutable variables are declared with the keyword val, which is similar to the final keyword in the Java language

object ScalaVariable {
    def main(args: Array[String]): Unit = {
        // User name
        val username : String = "zhangsan"
        username = "lisi" // Error
        username = true // Error
    }
}

val uses more

2.5 identifier

Scala can use two forms of flags, characters, numbers, and symbols.

  • Characters and numbers begin with letters or underscores and can be followed by letters or numbers. The symbol "$" is also regarded as a letter in Scala. However, identifiers starting with "$" are reserved flags generated by the Scala compiler. Applications should avoid using identifiers starting with "$" to avoid conflicts.
  • Scala's naming convention adopts the camel naming convention similar to Java. The first character is lowercase, such as toString. The first character of the class name is still capitalized. In addition, the use of flags ending with an underscore should also be avoided to avoid conflicts.
  • Scala internal implementation will use escape flags, such as: - > use $colon $minus $greater to represent this symbol.
/ and Java Same identifier naming rules
val name = "zhangsan" // OK
val name1 = "zhangsan0"   // OK
//val 1name = "zhangsan0" // Error
val name$ = "zhangsan1" // OK
val $name = "zhangsan2" // OK
val name_ = "zhangsan3" // OK
val _name = "zhangsan4" // OK
val $ = "zhangsan5"     // OK
val _ = "zhangsan6"     // OK
//val 1 = "zhangsan6"     // Error
//val true = "zhangsan6"  // Error

// Different identifier naming rules from Java
val + = "lisi" // OK
val - = "lisi" // OK
val * = "lisi" // OK
val / = "lisi" // OK
val ! = "lisi" // OK
//val @ = "lisi" // Error
val @@ = "lisi" // OK
//val # = "lisi" // Error
val ## = "lisi" // OK
val % = "lisi" // OK
val ^ = "lisi" // OK
val & = "lisi" // OK
//val ( = "lisi" // Error
//val ( = "lisi" // Error
//val ) = "lisi" // Error
//val = = "lisi" // Error
val == = "lisi" // OK
//val [ = "lisi" // Error
//val ] = "lisi" // Error
//val : = "lisi" // Error
val :: = "lisi" // OK
//val ; = "lisi" // Error
//val ' = "lisi" // Error
//val " = "lisi" // Error
val "" = "lisi" // OK
val < = "lisi" // OK
val > = "lisi" // OK
val ? = "lisi" // OK
val | = "lisi" // OK
val \ = "lisi" // OK
//val ` = "lisi" // Error
val ~ = "lisi" // OK
val :-> = "wangwu" // OK
val :-< = "wangwu" // OK
// Remember, being able to declare and be able to use are two different things

Posted by EvilCoatHanger on Fri, 29 Oct 2021 19:02:25 -0700