×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Misc. Topics

Scala Constructs

By IncludeHelp Last updated : October 10, 2024

Constructs in Scala

Constructs in a programming language are the building blocks or basic programming concepts that are defined in it.

Types of Scala Constructs

In Scala Programming Language, the constructs defined are,

  • Expression
  • Values and variables
  • Functions
  • Methods
  • Blocks
  • Classes and objects
  • Traits
  • Fields
  • Closures
  • Main method

Let's discuss these in detail,

1) Expressions

A line of code that can be compiled and has some resultant value is expression. These are displayed on screen using println statements.

Example

A * B
"String"

Program to illustrate expressions in Scala

object MyClass {
    def main(args: Array[String]) {
        println(45 * 24332); 
        println("Scala programming Language!")
        println("My Score is " + (456.0*100.0/500.0) + "%")
    }
}

Output

1094940
Scala programming Language!
My Score is 91.2%

In the above code, we have illustrated the use of expression in Scala. Using the println statement, we have printed different expressions. Like, mathematical expression, string and concatenated values.

2) Values and Variables

In Scala values can be stored in order to use the resulting value of expression later or it another expression.

Scala program two type to store values,

(a) Values

These are constant holders to hold the result of an expression. You can store the result of an expression in value which is initialized as val. But you cannot change the value stored once.

Syntax

val value_name = "Value to be stored"

Program to illustrate the working of vals in Scala

object MyClass {
    def main(args: Array[String]) {
        val a = 45 * 24332
        println(a)
    
        val result = (456.0*100.0/500.0)
        println("My Score is " + result + "%")
    }
}

Output

1094940
My Score is 91.2%

In the above code, we have stored expression values in a variable using val initialization. You can see that though we have not used the variable more than once, we could not reuse it to store the value of the second expression as it is initialized as val which is not reusable.

(b) Variables

These are holders that store the results of an expression. They are declared using val keywords. The plus point in working with val declarations is that you can re-initialize the values in val i.e. you can re-assign the variable with another value.

Syntax

Creating a variable,
	var variable_Name = "expression"
Reassigning value, 
	variable_Name = "another_expression"

Program to illustrator the working of variables

object MyClass {
    def main(args: Array[String]) {
        var value = 45 * 24332.0
        println(value)
    
        value = (456.0*100.0/500.0)
        println("My Score is " + value + "%")
    }
}

Output

1094940.0
My Score is 91.2%

In the above code, we have used a var declaration to store the expression result and then we have re-initialized it to store another result. This is possible if we are using val declaration. But the type of both the expressions should be the same.

Some important point on variable in Scala:

3) Function

A function is a reusable chunk of code that can perform the operation on the value passed as a parameter.

Anonymous functions are special functions that are assigned to a variable.

Syntax

Function 
    (parameter: Type ) => return_Expression
Anonymous Function: 
    val variable_Name = (parameter: Type ) => return_Expression
Calling a function:
    variable_Name(valueToBePassed)

Example

val result = (mark: Float ) => mark*100/500

Program to illustrate the working of functions in Scala

object MyClass {
    def main(args: Array[String]) {
        var result = (marks: Double ) => marks*100 / 500 
    
        println("My Score is " + result(462.4) + "%")
    }
}

Output

My Score is 92.48%

Some important concepts on functions in Scala

4) Methods

A method is a block of code that can perform a certain amount of tasks. It is similar to a function. It is defined using the def keyword. It has a parameter list to accept values of performing operation on and the resultant value is also returned.

Syntax

def methodName (parameters) : (return Type) = {
    // statements to be executed…
}

Program to illustrate the working of methods

object MyClass {
    def calcResult(marks : Double) : (Double) = {
        return marks*100 / 500 
    }
    
    def main(args: Array[String]) {
        println("My Score is " + calcResult(462.4) + "%")
    }
}

Output

My Score is 92.48%

Some important concepts on method in Scala

5) Block

A block is referred to as multiple statements that are enclosed between curly braces.

Syntax

{
    // statement 1
    // statement 2
    ...
}

Program to illustrate the working of block in Scala

object MyClass {
    def main(args: Array[String]) {
        {   // this is a block in scala 
            val marks = 315.12
            val score = marks*100 / 500
            println("My Score is " + score + "%")
        }
    }
}

Output

My Score is 63.024%

6) Classes and Objects

A class is a blueprint for objects. It contains the definition of all the members of the class. There are two types of members of the class in Scala,

Fields: the variables in Scala that are used to define data in the class.

Methods: The functions of a class that are used to manipulate the fields of the class and do some stuff related to the functioning of a class.

You can read more on classes here…

7) Traits

Traits in Scala are like interfaces in Java. A trait can have fields and methods as members, these members can be abstract and non-abstract while creation of trait.

More on traits...

8) Closure

Closures use one or more values that are not declared in the function to give the final output.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.