Skip to content
Related Articles

Related Articles

Improve Article
Scala Constructs
  • Last Updated : 22 Jul, 2020

Some of the basic Scala constructs are expressions, blocks, classes, objects, functions, methods, traits, Main methods, fields, and closures.
1. Scala Expression: A computable statement in Scala is known as expression. For example, the following is an expression: 3 + 4. To print the output of an expression println() is used.
Syntax

println(expression)

Examples




// Scala program to illustrate expressions
object Main 
{
    def main(args: Array[String]) 
    {
        println(3)
        println(3 + 4)
        println("Scala")
        println("How " + "are" + " you?"
    }
}

Output:

3
7
Scala
How are you?

2. Values: We can name it using the ‘val’ keyword. This expression returns a result.
Syntax

val variable_name = 'Result to be printed'

Examples






// Scala program to illustrate values
object Main 
{
    def main(args: Array[String]) 
    {
        val name1 ="Arjun"
        println(name1)
          
        val name2 = "Sheena"
        println(name2)
    }
}

Output

Arjun
Sheena

Here, this type of entity can have only one value. If we try to reassign the value, then it will give error.

3. Scala Variables: Variables are simply a storage location and it is known by its name and stores some known and unknown piece of information called a value. To declare a variable, we use the ‘var’ keyword.
Syntax

var variable_name = Any_number

Example




// Scala program to illustrate variables
object Main 
{
    def main(args: Array[String]) 
    {
        // Here, p is a variable
        var p = 3
        p = 4 
        println(p * p) 
    }
}

Output

16

4. Scala Block: A group of expressions that are delimited by curly braces({}) is called a block.
Syntax

println({// Expression})

Example




// Scala program to illustrate blocks
object Main 
{
    def main(args: Array[String]) 
    {
        println({val p = 10 * 2
                     p + 1}) 
    }
}

Output



21

5. Scala Class: A class is a user-defined blueprint or prototype from which objects are created. It contains values, variables, types, classes, functions, methods, objects, and traits that are collectively called members. To declare a class, we use the ‘class’ keyword and an identifier.

Syntax

{
    class class_name{}
    val variable_name = new class_name
}

Example




// Scala program to illustrate class
  
// class 
class Geeks 
        
    // Class variable
    var name: String = "GeeksforGeeks"
        
    // Class method 
    def Show() 
    
        println("Company's name : " + name); 
    
object Main  
        
    // Main method 
    def main(args: Array[String])  
    
            
        // Class object 
        var obj = new Geeks(); 
        obj.Show(); 
    

Output:

Company's name : GeeksforGeeks

6. Object: Object is a singleton of its own class i.e., it is a single instance of its own definition.
Strong:

object MyObject{
      def method_name() = {
      // Expression
  }
}

Example:




// Scala program to illustrate object
object Main extends App
{
    object MyObject
    {
        def plusthree() =
            val x = 2 + 2
            x + 3
        }
    }
    println(MyObject.plusthree) 
}

Output:

7

7. Scala Function: An expression that takes parameters are called functions. Or in other words, it is a collection of statements that perform a certain task. It can be assigned to a variable such type of function is known as an anonymous function. On the left of => is the list of parameters, and on the right is an expression it will return.

Syntax

(y:Int)=>y+y

This function takes an Integer argument y, and returns its addition. We can give the name to the value also.
Example:






// Scala program to illustrate function
object Main 
{
    def main(args: Array[String]) 
    {
        val addition = (y:Int) => y + y
        println(addition(4)) 
    }
}

Output:

8

8. Scala Method: We define method using the keyword “def”. It is almost similar to a function. It follows is an identifier, parameter lists, a return type, and a body. Or in other words, it is a collection of statements that perform a certain task.
Syntax

def method_name ([parameter_list]) : [return_type] = {
   
  // Method body

}

Example




// Scala program to illustrate method
object Main 
{
    def display()
    {
        println("Welcome to GFG")
    }
    def main(args: Array[String]) 
    {
        // Calling method
        display()
    }
}

Output

Welcome to GFG

9. Scala Trait: Traits are like interfaces in Java. But traits allow you to implement the members. It can have methods(both abstract and non-abstract), and fields as its members. It is defined using a keyword “trait”.
Syntax

trait trait_name
{
// Fields
// Methods
}

Example




// Scala program to illustrate traits 
  
// Trait 
trait MyTrait 
    def show
  
  
// MyGfg inherits a trait 
class MyGfg extends MyTrait 
      
    // Implementation of methods of MyTrait 
    def show() 
    
        println("Hey Geeks"
    
      
  
object Main 
      
    // Main method 
    def main(args: Array[String]) 
    
        val ob = new MyGfg(); 
        ob.show(); 
    

Output:

Hey Geeks

10. Scala Main Method: The entry point of any program is the main method. JVM needs a method with an argument which is an array of strings.
11. Fields: These fields help to define the object state. It is a unique set of instance variables that belong to each object.
12. Closure: Closure is a function whose return value depends on variables declared outside it.




My Personal Notes arrow_drop_up
Recommended Articles
Page :