
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Apply Method
- Scala - Update Methods
- Scala - UnapplySeq Method
- Scala - Inheritance
- Scala - Extending a Class
- Scala - Method Overloading
- Scala - Method Overriding
- Scala - Generic Classes
- Scala - Generic Functions
- Scala - Superclass Construction
- Scala Methods & Functions
- Scala - Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Lazy Val
- Scala - Pure Function
- Scala - Currying Functions
- Scala - Control Abstractions
- Scala - Corecursion
- Scala - Unfold
- Scala - Tail Recursion
- Scala - Infinite Sequences
- Scala - Dynamic Invocation
- Scala - Lambda Expressions
- Scala Collections
- Scala - Collections
- Mutable and Immutable Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - TreeMap
- Scala - SortedMap
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - Infinite Streams
- Scala - Parallel Collections
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Type Patterns
- Scala - Exception Handling
- Scala - Extractors
- Scala - Regular Expressions
- Scala Files I/O
- Scala - Files I/O
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala - BDD Testing
Scala - Keywords
Keywords are reserved words in Scala. Keywords are special reserved words which have predefined meanings and actions. No one can use these words as Identifiers, otherwise there will be compilation errors.
The following list shows the reserved words in Scala. These reserved words may not be used as constant or variable or any other identifier names.
Keywords | Description |
---|---|
abstract | Used to define abstract classes or traits. |
case | Used in pattern matching and for case classes. |
catch | Part of try-catch exception handling. |
class | Used to define a class. |
def | Used to define a method or function. |
do | Used in a do-while loop. |
else | Used in conditional expressions. |
extends | Used to indicate inheritance. |
false | A Boolean literal representing "false." |
final | Indicates that a class or method cannot be overridden or extended. |
finally | Part of try-catch-finally exception handling. |
for | Used to create loops and comprehensions. |
if | Used in conditional expressions. |
import | Used to import classes, objects, and methods from other packages. |
match | Used for pattern matching. |
new | Used to create new instances of classes. |
null | Represents a null value. |
object | Used to define a singleton object. |
package | Used to declare a package. |
private | Indicates that a member (class, method, etc.) is private. |
protected | Indicates that a member is protected. |
return | Used to return a value from a method. |
sealed | Used to restrict inheritance to a limited set of classes. |
super | Used to refer to a superclass. |
this | Used to refer to the current instance. |
throw | Used to throw an exception. |
trait | Used to define a trait. |
true | A Boolean literal representing "true." |
try | Used to start an exception handling block. |
type | Used to define type aliases. |
val | Used to declare an immutable (read-only) variable. |
var | Used to declare a mutable variable. |
while | Used in while loops. |
with | Used for mixin composition. |
yield | Used in for comprehensions to yield values. |
Example of Scala Keywords
This example program contains various above keywords to learn −
// Keywords Examples // abstract, class abstract class Shape { def area(): Double } // case, match case class Circle(radius: Double) extends Shape { def area(): Double = math.Pi * radius * radius } // catch, try try { val result = 10 / 0 } catch { case e: ArithmeticException => println("ArithmeticException caught") } // def def square(x: Int): Int = x * x // if, else val num = 42 if (num % 2 == 0) { println("Even") } else { println("Odd") } // import import java.util.{Date, Calendar} // match val day = 2 val dayName = day match { case 1 => "Monday" case 2 => "Tuesday" case _ => "Other" } // val val pi = 3.14159265 // var var counter = 0 counter += 1 // while var i = 0 while (i < 5) { println(i) i += 1 } // yield val numbers = List(1, 2, 3, 4, 5) val squares = for (n <- numbers) yield n * n // object object Greeting { def sayHello() = println("Hello, World!") } // final final class MyImmutableClass { // This class cannot be extended } // super class Parent { def display() = println("Parent class") } class Child extends Parent { def displayChild() = { super.display() println("Child class") } } // extends class Dog extends Animal // true, false val isSunny = true val isRainy = false
Scala Reserved Special Operators
Scala also has following reserved special operators −
Special Operators | Description |
---|---|
=> (Right Arrow) | Used to define function or method parameters and return types in function literals. |
<- (Left Arrow) | Used in for-comprehensions for pattern matching and value assignment. |
<: | Used in type bounds and variance annotations. |
:> | Used in type bounds. |
_ (Underscore) | Used as a wildcard in pattern matching and function literals. |
... (Ellipsis) | Used to represent a variable number of arguments in function parameters. |
< (Less Than) | Used in type parameter constraints. |
> (Greater Than) | Used in type parameter constraints. |
# (Pound) | Used in context bounds. |
@ (At) | Used in annotations. |
Example of Scala Reserved Special Operators
This example program contains various above special operators to learn −
// Right Arrow (=>) for function definitions val add: (Int, Int) => Int = (x, y) => x + y // Left Arrow (<-) for pattern matching val list = List(1, 2, 3) for { number <- list } println(s"Number: $number") // Less Than (<) and Greater Than (>) for comparisons val lessThan = 5 < 10 val greaterThan = 10 > 5 // Pound (#) for type bounds def foo[A <: Number](x: A) = println(s"Received: $x") // At (@) for annotations @deprecated("This method is deprecated", "1.0") def deprecatedMethod() = println("This is a deprecated method") // Underscore (_) for wildcards and placeholders val numbers = List(1, 2, 3, 4, 5) val squares = numbers.map(_ * _) // Ellipsis (...) for varargs in method definition def printArgs(args: Any*) = args.foreach(println) // Colon and Right Angle (:>) for type annotations val x: Int = 42 // Using symbols within identifiers val x_2: Int = 10 val foo_bar = "Hello, World!"
Advertisements