Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

Scala | Literals

Literals refer to fixed values in source code that can be assigned to variables or constants. In Scala, as in most programming languages, there are different types of literals corresponding to various data types.

Here's an overview of Scala literals:

  1. Integer Literals

    • Decimal: 42
    • Hexadecimal: 0x2A
    • Octal: 052 (Scala 2 only; removed in Scala 3)
  2. Long Literals

    • Appending L or l makes the integer a Long: 42L
  3. Floating Point Literals

    • Floating-point numbers: 3.14
    • Doubles: 3.14D or 3.14d
    • Floats: 3.14F or 3.14f
  4. Character Literals

    • Single characters in single quotes: 'a'
    • Escape sequences: '\n' (newline), '\t' (tab), '\'' (single quote), '\\' (backslash)
  5. String Literals

    • Regular strings in double quotes: "Hello, World!"
    • Strings with escape sequences: "This is a \"quote\"."
    • Raw strings using triple quotes, which can span multiple lines and don't process escape sequences:
      """This is a raw string.
         It spans multiple lines."""
      
  6. Boolean Literals

    • Two possible values: true and false
  7. Symbol Literals

    • Symbols are unique and immutable, written as 'symbolName
  8. Multi-line Strings and String Interpolation

    • Strings can span multiple lines using triple quotes. Additionally, you can interpolate values into a string using the s interpolator.
      val name = "Alice"
      val age = 30
      val multiLine = s"""Name: $name
                          |Age: $age""".stripMargin
      
  9. Null Literal

    • Represents the absence of a value or a null reference: null (Note: Using null is discouraged in functional programming. Options or other monadic constructs are preferred.)
  10. Function Literals (Lambda Functions)

    • Anonymous functions, also known as lambda functions or function literals:
      val add = (x: Int, y: Int) => x + y
      

In addition to these, Scala 3 introduces new literal types and some changes, especially with string interpolation, pattern matching, and the new optional braces syntax. It's always a good idea to consult the specific version's documentation to understand the nuances.

  1. Scala Literal Types:

    Literal types in Scala represent values directly, as opposed to types that represent sets of values.

    val myNumber: 42 = 42
    
  2. Numeric Literals in Scala:

    Numeric literals represent numbers in various formats.

    val integerLiteral: Int = 42
    val floatingPointLiteral: Double = 3.14
    
  3. String Literals in Scala:

    String literals represent sequences of characters.

    val myString: String = "Hello, Scala!"
    
  4. Boolean Literals in Scala:

    Boolean literals represent the logical values true or false.

    val myBoolean: Boolean = true
    
  5. Character Literals in Scala:

    Character literals represent single characters enclosed in single quotes.

    val myChar: Char = 'A'
    
  6. Symbol Literals in Scala:

    Symbol literals represent unique identifiers and are written as '<identifier>.

    val mySymbol: Symbol = 'mySymbol
    
  7. Literal Values vs Variables in Scala:

    Literal values are fixed and cannot be changed, while variables can be reassigned.

    val literalValue: Int = 42
    var myVariable: Int = 42
    
    // This will result in a compilation error
    // literalValue = 43
    
    // This is valid
    myVariable = 43
    
  8. Literal Interpretation in Scala:

    Scala provides a way to interpret string literals with the s, f, and raw interpolators.

    val name = "Alice"
    val greeting = s"Hello, $name!"
    

    The s interpolator allows variable substitution within the string.