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
// 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
// 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
// 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
// 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
// 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
// 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
// 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
// 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
// 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.
Similar Reads
Scala | Product3
Product3 is a trait in Scala, which is a Cartesian product of three elements. The Linear Supertypes here are Product, Equals, and Any and the known subclass here is Tuple3. It extends the trait Product i.e, trait Product3[+T1, +T2, +T3] extends Product Where, T1, T2, and T3 are the used parameter ty
2 min read
Introduction to Scala
Scala is a general-purpose, high-level, multi-paradigm programming language. It is a pure object-oriented programming language which also provides the support to the functional programming approach. There is no concept of primitive data as everything is an object in Scala. It is designed to express
7 min read
Scala Parser Combinators
When a parser generator is required, some famous parsers that cross our mind are: Yacc and Bison for parsers written in C and ANTLR for parsers written in Java but they are designed to run for specific programming languages. This shortens the scope of use of parsers. However, Scala provides a unique
3 min read
Monads in Scala
In Scala, Monads is a construction which performs successive calculations. It is an object which covers the other object. It is worth noting that here, the output of an operation at some step is an input to another computations, which is a parent to the recent step of the program stated. Monad is ne
4 min read
How to Execute OS Commands in Scala?
Scala is a versatile programming language. It offers smooth approaches to running OS instructions, whether or not you want to deal with documents, automate system operations, or communicate with external gear. This article focuses on discussing ways to execute OS commands in Scala. PrerequisitesInst
2 min read
Interesting fact about Scala
Scala(pronounced as "skah-lah") is general-purpose programming language designed by Martin Odersky. The design of Scala started in 2001 at EPFL, Lausanne, Switzerland. Scala was released publicly in 2004 on Java platform. Scala is designed to be concise and addresses criticisms of Java. Scala source
3 min read
Scala Product Map Values
In Scala the product of the map elements can be done by utilizing foldLeft method. Syntax: m1.foldLeft(1)(_*_._1) Here, m1 is used for a Map, foldLeft is a method that takes an initial value one for product. it will take previous result and multiply it to the next map key value. * and use 1 for the
1 min read
Scala Long ==(x: Char) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The ==(x: Char) method is utilized to return true if this value is equal to x, false otherwise. Method Definition - def ==(x: Char): Boolean Returns - Returns true if this value is equal to x, false otherwi
1 min read
How to Install Scala in Linux?
Prerequisite: Introduction to Scala Before, we start with the process of Installing Scala on our System. We must have first-hand knowledge of What the Scala Language is and what it actually does? Scala is a general-purpose, high-level, multi-paradigm programming language. It is a pure object-oriente
3 min read
Scala | Reduce, fold or scan
In this tutorial we will learn about Reduce, Fold and Scan functions in Scala. Reduce : Reduce function is applied on collection data structure in scala that contains lists, sets, maps, sequence and tuples. Parameter in the reduce function is a binary operation which merges all the elements from the
5 min read