Scala Literals

In Scala, literals are expressions that directly represent fixed values.

Scala supports various types of literals, including numeric, character, string, boolean, etc.

Integer Literals

Integer literals are used for the Int type. To represent Long, you can add L or lowercase l as a suffix after the number.

Integer literals can be decimal, hexadecimal, and octal.

  • Decimal:42, 0, -99
  • Hexadecimal: using0xor0Xas prefix, such as0x2A, 0x0, 0XFF
  • Octal: Before Scala 2.10, octal literals were supported, using0as prefix, but octal literals were removed in Scala 2.10 and later versions.
val decimal = 42
val hex = 0x2A

Floating-point Literals

If a floating-point number is followed by an f or F suffix, it represents a Float type; otherwise, it is a Double type.

  • Double precision:3.14, -0.001, 1.0e-10
  • Single precision:3.14f, -0.001F
val doubleValue = 3.14
val floatValue = 3.14f

Boolean Literals

Boolean literals have true and false.

val isScalaFun = true
val isSkyGreen = false

Symbol Literals

Symbol literals are written as:'<identifier>, here<identifier>can be any alphanumeric identifier (note: it cannot start with a digit). This literal is mapped to an instance of the predefined class scala.Symbol.

For example: symbol literal'xis shorthand for the expressionscala.Symbol("x")symbol literals are defined as follows:

package scala
final case class Symbol private (name: String) {
   override def toString: String = "'" + name
}

Character Literals

In Scala, character variables use single quotes'to define, as follows:

'a' 
'\u0041'
'\n'
'\t'

Where\represents an escape character, which can be followed byu0041digits or\r\nand other fixed escape characters.

String Literals

In Scala, string literals use double quotes"to define, as follows:

"Hello,\nWorld!"
"Example官网:www.example.com"
  • Normal string:"Hello, World!"
  • Multiline string: use triple quotes"""to enclose, can contain newlines and quotation marks.

Representation of Multiline Strings

Multiline strings use three double quotes as delimiters, the format is:""" ... """。

The example is as follows:

val foo = """Example
www.example.com
www.runnoob.com
以上三个地址都能访问"""

Null Value

Null is a special literal that represents the null value of all reference types.

The null value is of type scala.Null.

Scala.Null and scala.Nothing are special types that handle certain 'edge cases' of Scala's object-oriented type system in a unified way.

The Null class is the type of the null reference object; it is a subclass of every reference class (classes inheriting from AnyRef). Null is not compatible with value types.

val nullValue: String = null

Unit

The Unit type has only one instance value, using the literal()to represent, similar to void in Java.

val unitValue: Unit = ()

Example

The following is sample code using different types of literals:

Example

object LiteralsExample {
  def main(args: Array[String]): Unit = {
    // numeric literals
    val decimal = 42
    val hex = 0x2A
    val doubleValue = 3.14
    val floatValue = 3.14f
   
    // character literals
    val charA = 'A'
    val charNewLine = '\n'
   
    // string literals
    val greeting = "Hello, World!"
    val multilineString = """This is
                            |a multi-line
                            |string"
"".stripMargin
   
    // boolean literals
    val isScalaFun = true
    val isSkyGreen = false
   
    // special literals
    val nullValue: String = null
    val unitValue: Unit = ()
   
    // output example
    println(s"Decimal: $decimal, Hex: $hex")
    println(s"Double: $doubleValue, Float: $floatValue")
    println(s"Char: $charA, New Line: $charNewLine")
    println(s"Greeting: $greeting")
    println(s"Multiline String: $multilineString")
    println(s"Is Scala Fun: $isScalaFun, Is Sky Green: $isSkyGreen")
    println(s"Null Value: $nullValue, Unit Value: $unitValue")
  }
}

The result of compiling and executing the above code is:

Decimal: 42, Hex: 42
Double: 3.14, Float: 3.14
Char: A, New Line: 

Greeting: Hello, World!
Multiline String: This is
a multi-line
string
Is Scala Fun: true, Is Sky Green: false
Null Value: null, Unit Value: ()
Other Extensions