ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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,

  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.

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
                    ^
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.