Home »
Scala
Scala Keywords
Scala | Keywords: Here, we will learn about various keywords in Scala with examples.
Submitted by Shivang Yadav, on February 02, 2021
Keywords
Keywords are also known as reserved words, keywords are special words for a programming language that have some predefined actions in the programming. Thus, these keywords cannot be used as names for defining objects, classes, functions, variables, etc. The compiler will throw an error if we try to use keywords as identifiers.
Keywords in Scala
Scala programming languages also consist of a set of keywords to help in the efficient working of the program.
There are in total 39 keywords present in Scala. Here is a list of all keywords in Scala,
- abstract
- case
- catch
- class
- def
- do
- else
- extends
- false
- final
- finally
- for
- forsome
- if
- implicit
- import
- lazy
- match
- new
- null
- object
- override
- package
- private
- protected
- return
- sealed
- super
- this
- throw
- trait
- try
- true
- type
- val
- var
- while
- with
- yield
Other than data types are also reserved in Scala.
Example 1: Program to illustrate the use of keyword as identifier (This will return an error)
object MyClass {
def main(args: Array[String]) {
val class = 315.12
val score = class*100 / 500
println("My Score is " + score + "%")
}
}
Output:
scala:3: error: illegal start of simple pattern
val class = 315.12
^
scala:4: error: '=' expected.
val score = class*100 / 500
^
scala:4: error: illegal start of simple expression
val score = class*100 / 500
^