Capsules for recovering Scala fever.


Scala is a multi-paradigm programming language that combines the features of object-oriented programming and functional programming. The word “Scala” refers to “Sca(Scalable)+la(Language)” i.e designed to grow with the demands of its users. Scala runs on the Java Virtual Machine.

Here We’ll discuss about some of the basic concepts in Scala that make it distinct from other programming languages.Let us start with some of the following:


1. Simple method in scala:-

object Check {
   def countcharacter(a: String, b: String) = { //Declaring a method in scala
       if (a.length > b.length) println("The large word is" + a) 
       else  println("The large word is" + b) 
 }

  def main(args: Array[String]) {                 // The main method in scala
      countcharacter("Neelkanth","Sachdeva")     //calling the method
  }

}



2. Scala Array with “for” loop :-

object forloopdemo {
  
  val name= new Array[String](4)     // Declaring an Array in scala
  name(0)="This"
    name(1)="is"
      name(2)="an"
        name(3)="array"
  
  def printarrayelem(args:Array[String]):Unit={  

   /*Creating a method named "printarrayelem" with return type "Unit",
   that will print out all the elements in array.  */                                             
                                                     
  for(arg <- args)      
  /* A sample for loop for printing all elements in array. */                                                  
  println(arg)         
}
  
  def main(args: Array[String]) {    //The main method in scala
  printarrayelem(name)
  }
}

3. Constructor in Scala:-


class construct(val name: String, val age: Int) {                
println("This is the example program of primary constructor")
}

object PrimaryConstructor {
  def main(args: Array[String]) {           //the main method.
    val cons = new construct("Neelkanth Sachdeva", 23) 
     /*passing the values for name and age at the time of object creation. */                                                
                                                       
    println(cons.name)      // accessing the value via constructor.
  }
}

4. Companion Objects. :-

In Java, you often have a class with both instance methods and static methods. In Scala, you achieve this by having a class and a “companion” object
of the same name. The class and its companion object can access each other’s private features. They must be located in the same source file.

class companion{
  private var balance=0.0
  private def deposit(amount:Double){      //Declaring a private method
  balance+=amount
  println(balance)
  }
}
object companion {
  def main(args: Array[String]) {
  val n=new companion
/*Accessing the private method of class in its companion object.  */
  n.deposit(78.00)                
   }
 }

5. Bypass the main method :-
In Scala, you need not to have main method always, you can bypass the main method by extending the “App” trait as follows.


object main_alternate extends App{       //extending the "App" trait 
                                         //to avoid main method.
 println("This is the alternate of main")
}

6.ArrayBuffer in Scala:-
Most operations on an array buffer have the same speed as for an array, because the operations simply access and modify the underlying array. Additionally, array buffers can have data efficiently added to the end.Array buffers are useful for efficiently building up a large collection whenever the new items are always added to the end.

import scala.collection.mutable.ArrayBuffer
object arraybuffdemo {
  val abuf = new ArrayBuffer[Int]()        //Array buffer
  val array = Array(1, 2, 3, 4, 5)         //Simple array

  def main(args: Array[String]) {

    abuf += 1            //Inserting an element to buffer
    println(abuf)

    abuf ++= array      //Inserting a whole array to arraybuffer
    println(abuf)

    abuf.trimStart(1)   //removing one element from 
                         //starting point in arraybuffer
    println(abuf)

    abuf.trimEnd(1)    //removing one element from 
                         //ending point in arraybuffer
    println(abuf)

    abuf.insert(1, 80, 81, 82)  //inserting the three element 
                                //say 80,81,82 at index 1. 
    println(abuf)
       
    abuf.remove(1, 2)   //removing two elements from the index 1
    println(abuf)

 }
}

Enjoiii Scala!!!! 🙂

Advertisements

About Neelkanth Sachdeva

Project Manager @ eCIFM Solutions Inc. Previous : Senior Software Engineer @ Evernym Inc. Previous : Software Consultant @ Knoldus Software LLP Software Consultant @ Inphina Technologies
This entry was posted in News, Scala, Web and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s