JAVA DEVELOPER SITE
 

Scala - Basic Syntax


In this chapter Scala - Basic Syntax If you understanding on Java it will pretty easy to learn Scala syntax

Object − Objects have states and behaviors. An object is an instance of a class. Example − A cat has states - color, name, breed as well as behaviors - wagging, meow, and eating.

  • Class − A class can be defined as a template/blueprint that describes the behaviors/states that are related to the class.

  • Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.

  • Fields − Each object has its unique set of instance variables, which are called fields. An object's state is created by the values assigned to these fields.

  • Closure − A closure is a function, whose return value depends on the value of one or more variables declared outside this function.

  • Traits − A trait encapsulates method and field definitions, which can then be reused by mixing them into classes. Traits are used to define object types by specifying the signature of the supported methods.

  • First Scala Program

    Scala have two modes:interactive mode & script mode.

    Interactive Mode

    Open the command prompt and use the following command to open Scala.
    \>scala
    
    If Scala is installed in your system, the following output will be displayed −
    Welcome to Scala version 2.9.0.1
    Type in expressions to have them evaluated.
    Type :help for more information.
    
    the right of the Scala prompt and press the Enter key
    scala> println("Hello, Scala!");
    
    It will produce the following result −
    Hello, Scala!
    

    Script Mode

    Use the following instructions to write a Scala program in script mode
    object HelloWorld {
       /* This is my first java program.  
       * This will print 'Hello World' as the output
       */
       def main(args: Array[String]) {
          println("Hello, world!") // prints Hello World
       }
    }

    Save the file as − HelloWorld.scala.

    \> scalac HelloWorld.scala
    \> scala HelloWorld
    

    Output

    Hello, World!
    

    Scala Keywords

    There are following list of scala keyword.
    abstract case catch class
    def do else extends
    false final finally for
    forSome if implicit import
    lazy match new Null
    object override package private
    protected return sealed super
    this throw trait Try
    true type val Var
    while with yield  
    - : = =>
    <- <: <% >:
    # @

    Comments in Scala

    Scala supports single-line and multi-line comments very similar to Java.
    object HelloWorld { /* This is my first java program. * This will print 'Hello World welookups' as the output * This is an example of multi-line comments. */ def main(args: Array[String]) { // Prints Hello World // This is also an example of single line comment. println("Hello, world! welookups") } }

    Blank Lines and Whitespace

    A line containing only whitespace, possibly with a comment, is known as a blank line, and Scala totally ignores it.

    Newline Characters

    Scala is a line-oriented language where statements may be terminated by semicolons (;) or newlines. val s = "hello"; println(s)

    Scala Packages

    A package is a named module of code. For example, the Lift utility package is net.liftweb.util.
    package com.liftcode.stuff
    
    Scala packages can be imported so that they can be referenced in the current compilation scope.
    import scala.xml._
    
    You can import a single class and object, for example, HashMap from the scala.collection.mutable package
    import scala.collection.mutable.HashMap
    
    You can import more than one class or object from a single package import scala.collection.immutable.{TreeMap, TreeSet}

    Apply Dynamic

    Instances x of this trait allow method invocations x.meth(args) for arbitrary method names meth and argument lists args as well as field accesses x.field for arbitrary field namesfield.
    foo.method("blah") ~~> foo.applyDynamic("method")("blah")
    foo.method(x = "blah") ~~> foo.applyDynamicNamed("method")(("x", "blah"))
    foo.method(x = 1, 2) ~~> foo.applyDynamicNamed("method")(("x", 1), ("", 2))
    foo.field ~~> foo.selectDynamic("field")
    foo.varia = 10 ~~> foo.updateDynamic("varia")(10)
    foo.arr(10) = 13 ~~> foo.selectDynamic("arr").update(10, 13)
    foo.arr(10) ~~> foo.applyDynamic("arr")(10)