Scala 中的分号

Scala分号

分号或分号(;)是在编程一个标点符号,它是用来单独的代码多条线。它在C,C ++,Java,Pascal等主要编程语言中很常见。在现代编程语言(如Python,Scala)中。

分号被视为行语句结束。它将其视为表达式结尾,否则表达式可以继续到下一行。但是,在Scala中,该行的结尾被视为expression的结尾。这意味着用户不需要强制使用分号(;)语句。

语法:

    Code with a semicolon :     var a : int = 3445; 
    Code without semicolon:     var a : int = 3445

示例

object MyClass {
      def main(args: Array[String]) {
         println("This statement is executed with semicolon");
         println("This statement is executed without semicolon")         
      }
   }

输出结果

This statement is executed with semicolon
This statement is executed without semicolon

代码逻辑:

此处的代码有两个打印语句,一个打印语句以分号结尾,第二个不带分号。这两行均有效,并打印给定的字符串。