Scala


Scala is a multi paradigm programming language used for building scalable application. Applications build on Scala are - Secure, Faster,  Reliable, Resistance to Run time exception



  • About Scala
    • Scala is scalable, functional, immutable and most importantly uses less piece of code to achieve a task.
    • The Scala compiler generates byte code which is identical to that generated by the Java compiler.
    • Scala code can be de-compiled to Java code (With the exception of certain constructor operations).
    • To the JVM, both java code and Scala code looks similar with the only difference is a run time scala-library.jar library.
  • Getting Started with Scala
    • Installation And Setup
      • Download the Java Run time Environment 1.6 or later (1.8 recommended) from - http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
      • Add the \bin directory to the PATH system environment variable.
      • Download Scala IDE for Eclipse (Download Scala IDE)
    • Create SBT (Scala Build Tool) Scala Project
      • Download SBT (SBT)
      • Run the installer
      • Add /bin directory to the PATH system environment variable 
      • Verify the SBT working by typing sbt -verson in the command line
  • Scala Features
    • Object
      • In Scala Singleton immutable objects are declared using object instead of class.
      • Scala doesn’t have static class members (variables or methods). Instead, it has singleton objects, which are classes with only one object in the class.
      • To achive static in scala, it is common to place static members (variables or methods) in a singleton object.
      • We can define apply(), unapply() and update() function methods in an Object. (Refer : apply(), unapply() and update() function with Object example)
      • Overloaded Object Constructors can be defined with the help of apply() method
      • We can’t use this keyword in a singleton object definition.
      • This this keyword is used for defining constructors in a class.
      • Objects or Traits may not have parameters.
                          Ex –
                      object Student(a: Int){} // Error
                      object Student(){} // Error
                                       object Student{} // Success
    • Object Constructor :
               object ScalaUser {
                def apply() = {
                 println("No argument constructor.....")
                }
                def apply(a: Int) = {
                 println("One argument constructor.....")
                }
                def print() = {
                 println("print call.....")
                }
              }

              object Client extends App {
               ScalaUser.print()
               var noArgument = ScalaUser()
               var oneArgument = ScalaUser(2)
              }

              Output :
              print call.....
              No argument constructor.....


              One argument constructor.....
    • Class
      • Scala has class parameters in place of constructor parameters similar to parameters to a function.
      • We can define apply() and update() function methods in a class as well. (Refer : apply() and update() function with Class example)
      • Overloaded Class constructors can be defined with the help of this() method (Refer : Class Constructor section
    • Constructor
      • Class Constructor :
                class ScalaUser(email: String, password: String, token: String) {
                  def this() {
                    this("", "", "")
                    println("No argument constructor.....")
                  }
                  def this(obj1: String) {
                    this(obj1, obj1, obj1)
                    println("One String argument constructor.....")
                  }
                  def this(obj1: Int) {
                    this("", "", "")
                    println("One Int argument constructor.....")
                  }
                  def this(obj1: String, obj2: String) {
                    this(obj1, obj2, obj1)
                    println("Two argument constructor.....")
                  }
                 def this(obj1: String, obj2: String, obj3 : Int) {
                    this(obj1, obj2, obj1)
                    println("Three argument constructor.....")
                 }
               }

               object Client extends App {
                var user = new User
                var oneStr = new User("one")
                var oneInt = new User(1)
                var two = new User("one", "two")
                var theree = new User("one", "two", "three")
               }
              
               Output :
               No argument constructor.....
               One String argument constructor.....
               One Int argument constructor.....
               Two argument constructor.....

Comments

Popular posts from this blog

Null check of Object and Object Chain in Java

Functional Programming