Scala statements interpretation(Read–Eval–Print Loop (REPL))


    Go to Scala .
    Type the following .
    scala_locatin
    Testing the following ..

  1. Expressions
  2. Expressions are computable statements.

    1 + 1
    

    expers1

    You can output results of expressions using println.

    println(1) // 1
    println(1 + 1) // 2
    println("Hello!") // Hello!
    println("Hello," + " world!") // Hello, world!
    

    scalaprint

  3. Values
  4. You can name results of expressions with the val keyword.

    val x = 1 + 1
    println(x) // 2
    Named results, such as x here, are called values. Referencing a value does not re-compute it.
    

    val1
    Values cannot be re-assigned.

    val x = 1 + 1
    x = 3 // This does not compile.
    

    val2
    Types of values can be inferred, but you can also explicitly state the type, like this:

    val x: Int = 1 + 1
    

    Notice how the type declaration Int comes after the identifier x. You also need a :.
    val3

  5. Variables
  6. Variables are like values, except you can re-assign them. You can define a variable with the var keyword.

    var x = 1 + 1
    x = 3 // This compiles because "x" is declared with the "var" keyword.
    println(x * x) // 9
    

    var2
    As with values, you can explicitly state the type if you want:

    var x: Int = 1 + 1
    

    var3

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *