Scala Keywords

In this tutorial, we will learn about the various keywords used in Scala with examples. By Shivang Yadav Last updated : April 02, 2023

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.

List of Scala Keywords

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,

  1. abstract
  2. case
  3. catch
  4. class
  5. def
  6. do
  7. else
  8. extends
  9. false
  10. final
  11. finally
  12. for
  13. forsome
  14. if
  15. implicit
  16. import
  17. lazy
  18. match
  19. new
  20. null
  21. object
  22. override
  23. package
  24. private
  25. protected
  26. return
  27. sealed
  28. super
  29. this
  30. throw
  31. trait
  32. try
  33. true
  34. type
  35. val
  36. var
  37. while
  38. with
  39. yield

Other than data types are also reserved in Scala.

Scala Keyword Example

This example illustrates the use of keyword as identifier (This will return an error)

Scala example to use keyword as an identifier

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
                    ^

Scala Keywords Related Questions


How many keywords are there in Scala?

There is total 39 keywords are there in Scala.

Is type a keyword in Scala?

Yes, type is a keyword in Scala.

Is static a keyword in Scala?

No, static is not a keyword in Scala.

Does Scala have a public keyword?

No, Scala doesn't have a public keyword






Comments and Discussions!

Load comments ↻






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