Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the basic syntax of Scala

Shulou Source: shulou.com Published: 2022-06-02 07:22:05 09月16日 Update

This article introduces the relevant knowledge of "what are the basic grammars of Scala". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Hello, HelloScala.

Object FunctionApp {def main (args: Array [String]): Unit = {println ("HelloScala.")}}

two。 Basic definition of function / method

Object FunctionApp {def main (args: Array [String]): Unit = {/ / call function println (add (10helm 5)) sayhello () sayhello / / can also be called without parentheses In special cases, you must enter the parameter def add (x:Int) with parentheses / /. Y:Int): Int = {x * y} def sayhello (): Unit = {println ("I don't have a return value.")} / / the special case must be called with parentheses def xm (name:String= "Zhang San"): Unit = { Println (name)} xm () xm ("Li Si")}

3. Conditional expression

Method 1: val x = 100 val a = if (x > 0) {true} else {false} println (a) method 2: val x = 100 val a = if (x > 0) true else false println (a)

4. Cycle mode

/ / left closed and right closed 1 to 10 = 1.to (10) scala.conllection.immutable.Rang.Inclusive = Rang (1 until 2, 3 4 scala.conllection.immutable.Rang.Inclusive = Rang 6 7, 8, 9) / / left close and right open (source code call is also a Rang method) 1 until 10 = 1.until (10) scala.conllection.immutable.Rang.Inclusive = Rang (1 until 10) / left closed and right opened Range (1) scala.conllection.immutable.Rang.Inclusive = Rang / left close and right open (1) The last bit indicates the step size or interval and can not be 0Range (1meme 10jin3) scala.conllection.immutable.Rang.Inclusive = Rang (1meme 4m 7) / / IDEA usage for (I)

Tags: Parameters methods situations parentheses mathematics Chinese grammar special content functions more conditions knowledge compulsion practicality achievement and then hello chemistry dilemma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Huawei Xiaomi macOS Linux