Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as variable names or objects. Doing this will result in a compile-time error.
Example:
object Main
{
def main(args : Array[String])
{
var p = 10
var q = 30
var sum = p + q
println( "The sum of p and q is :" +sum);
}
}
|
Output:
The sum of p and q is :40
Scala contains following keywords:
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 |
true |
try |
type |
val |
var |
while |
with |
yield |
>: |
⇒ |
=> |
= |
<% |
<: |
← |
<- |
# |
@ |
: |
_ |
Example:
class GFG
{
var name = "Priyanka"
var age = 20
var branch = "Computer Science"
def show()
{
println( "Hello! my name is " + name + "and my age is" +age);
println( "My branch name is " + branch);
}
}
object Main
{
def main(args : Array[String])
{
var ob = new GFG();
ob.show();
}
}
|
Output:
Hello! my name is Priyankaand my age is20
My branch name is Computer Science
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!