Scala | Literals

Any constant value which can be assigned to the variable is called as literal/constant. The literals are a series of symbols utilized for describing a constant value in the code. There are many types of literals in Scala namely Character literals, String literals, Multi-Line String literals, Boolean literals, Integer literals, and Floating point literals.

Types of literals

Integer Literals: The Integer literals are generally of type Int or of type Long when a suffix L or l is added at the end of the Integers. The type Int, as well as type Long, are all Integer numerals.

Note:

• The range of the type Int is from (-2^31 to 2^30).
• The range of the type Long is from (-2^63 to 2^62).
• When an Integer literal has a number that falls out of this range then a compile-time error arises.

Decimal literals: Here, the allowed digits are from 0 to 9.

`val x = 37`

Hexa-decimal literals: Here, the allowed digits are from 0 to 9 and characters used are from a to f. We can use uppercase as well as lowercase characters.

```// The hexa-decimal number should be prefix
// with 0X or 0x.
val x = 0xFFF```

Floating Point Literals: This type of literals are of type Double as well as type Float when a suffix F or f is added at the end and we can even specify Double type by suffixing with d or D.

`val x = 3.14159`

Example:

Scala

 `// Scala program of floating``// point literals` `// Creating object``object` `double``{` `    ``// Main method``    ``def` `main(args``:` `Array[String])``    ``{` `        ``// decimal-form literal``        ``val` `a ``=` `3.156` `        ``// It is also a decimal ``        ``// form of the literal``        ``val` `b ``=` `0123.34` `        ``// Displays results``        ``println(a)``        ``println(b)``    ``}``}`

Output:
```3.156
123.34```

Here, we can’t specify literals in Octal or Hexadecimal form.

Character Literals:

character literals are either uni-code character which are printable or are represented by escape sequences.

```val x = 'b'
//character literal in a single quote.```
```val x = '\u0051'
//uni-code representation of character literal,
//This uni-code represents Q.```
```val x = '\n'
//Escape sequence in character literals```

Example:

Scala

 `// Scala program of character``// literal` `// Creating object``object` `literal``{` `    ``// Main method``    ``def` `main(args``:` `Array[String])``    ``{``        ``// Creating a character literal``        ``// in single quote``        ``val` `x ``=` `'b'` `        ``// uni-code representation of``        ``// character literal``        ``val` `y ``=` `'\u0051'` `        ``// Escape sequence in character``        ``// literals``        ``val` `z ``=` `'\n'` `        ``// Displays results``        ``println(x)``        ``println(y)``        ``println(z)``    ``}``}`

Output:
```b
Q```

The character literals are enclosed in a single quote.

String literals: The String literals are a series of characters, which are available in double-quotes. The String literals can be handled smoothly by utilizing String Interpolation.

`val x = "GfG"`

Example:

Scala

 `// Scala program of literals` `// Creating object``object` `literal``{` `    ``// Main method``    ``def` `main(args``:` `Array[String])``    ``{``        ``// Creating a string ``        ``// literal``        ``val` `x ``=` `"GeeksforGeeks"` `        ``// Displays string ``        ``// literals``        ``println(x)``    ``}``}`

Output:
`GeeksforGeeks`

Single-line string literals are enclosed in a quotation marks.

Multi-Line String Literals: The multi-line string literals are also a series of characters but it has multiple lines.

`val x = """GfG"""`

Example:

Scala

 `// Scala program of multi-line``// string literals` `// Creating object``object` `literal``{` `    ``// Main method``    ``def` `main(args``:` `Array[String])``    ``{``        ``// Creating a multiple ``        ``// line string literal``        ``val` `x ``=``"""GeeksforGeeks``        ``is a``        ``computer science ``        ``portal"""` `        ``// Displays multiple``        ``// lines``        ``println(x)` `    ``}``}`

Output:
```GeeksforGeeks
is a
computer science
portal```

The multi-line string literals are enclosed in triple quotes.

Boolean Literals: Boolean literals allow only two values i.e. true and false, which are members of the type Boolean.

`val x = true`

Example:

Scala

 `// Scala program of Boolean``// literals` `// Creating object``object` `GfG``{` `    ``// Main method``    ``def` `main(args``:` `Array[String])``    ``{` `        ``// Assigning true``        ``val` `a ``=` `true` `        ``// Assigning false``        ``val` `b ``=` `false` `        ``// Displays results``        ``println(a)``        ``println(b)``    ``}``}`

Output:
```true
false```

Previous
Next