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
  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. */                                                  
  def main(args: Array[String]) {    //The main method in scala

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(      // 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
object companion {
  def main(args: Array[String]) {
  val n=new companion
/*Accessing the private method of class in its companion object.  */

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

    abuf ++= array      //Inserting a whole array to arraybuffer

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

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

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


Enjoiii Scala!!!! 🙂

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.