Scala | Literals

01 Apr, 2019
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.
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 (-231 to 230).The range of the type Long is from (-263 to 262).When an Integer literal has a number which falls out of this range then a compile time error arises. The Integers literals are specified in two ways:Decimal literals:Here, the allowed digits are from 0 to 9.val x = 37Hexa-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 = 0xFFFExample:// Scala program of integer// literals  // Creating objectobject integer{      // Main method    def main(args: Array[String])    {          // decimal-form literal        val a = 46          // Hexa-decimal form literal        val b = 0xfF          // Displays results in        // integer form        println(a)        println(b)    }}Output:46
255
Note: The Octal form of the literal is obsolete.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 suffixed with d or D.val x = 3.14159Example:// Scala program of floating// point literals  // Creating objectobject 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 literalsExample:// Scala program of character// literal  // Creating objectobject 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 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 program of literals  // Creating objectobject literal{      // Main method    def main(args: Array[String])    {        // Creating a string         // literal        val x = "GeeksforGeeks"          // Displays string         // literals        println(x)    }}Output:GeeksforGeeks
A single line string literals are enclosed in a quotation marks.Multi-Line String Literals :The multi-line string literals are also series of characters but it has multiple lines.val x = """GfG"""Example:// Scala program of multi-line// string literals  // Creating objectobject 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 type Boolean.val x = trueExample:// Scala program of Boolean// literals  // Creating objectobject 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

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 (-231 to 230).The range of the type Long is from (-263 to 262).When an Integer literal has a number which falls out of this range then a compile time error arises. The Integers literals are specified in two ways:Decimal literals:Here, the allowed digits are from 0 to 9.val x = 37Hexa-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 = 0xFFFExample:// Scala program of integer// literals  // Creating objectobject integer{      // Main method    def main(args: Array[String])    {          // decimal-form literal        val a = 46          // Hexa-decimal form literal        val b = 0xfF          // Displays results in        // integer form        println(a)        println(b)    }}Output:46
255
Note: The Octal form of the literal is obsolete.
The range of the type Int is from (-231 to 230).
The range of the type Long is from (-263 to 262).
When an Integer literal has a number which 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
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
// The hexa-decimal number should be prefix
// with 0X or 0x.
val x = 0xFFF
Example:
// Scala program of integer// literals  // Creating objectobject integer{      // Main method    def main(args: Array[String])    {          // decimal-form literal        val a = 46          // Hexa-decimal form literal        val b = 0xfF          // Displays results in        // integer form        println(a)        println(b)    }}
46
255

Note: The Octal form of the literal is obsolete.
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 suffixed with d or D.val x = 3.14159Example:// Scala program of floating// point literals  // Creating objectobject 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.
val x = 3.14159
Example:
// Scala program of floating// point literals  // Creating objectobject 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)    }}
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 literalsExample:// Scala program of character// literal  // Creating objectobject 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.
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 program of character// literal  // Creating objectobject 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)    }}
b
Q

The character literals are enclosed in a single quote.
String literals :The String literals are 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 program of literals  // Creating objectobject literal{      // Main method    def main(args: Array[String])    {        // Creating a string         // literal        val x = "GeeksforGeeks"          // Displays string         // literals        println(x)    }}Output:GeeksforGeeks
A single line string literals are enclosed in a quotation marks.
val x = "GfG"
Example:
// Scala program of literals  // Creating objectobject literal{      // Main method    def main(args: Array[String])    {        // Creating a string         // literal        val x = "GeeksforGeeks"          // Displays string         // literals        println(x)    }}
GeeksforGeeks

A single line string literals are enclosed in a quotation marks.
Multi-Line String Literals :The multi-line string literals are also series of characters but it has multiple lines.val x = """GfG"""Example:// Scala program of multi-line// string literals  // Creating objectobject 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.
val x = """GfG"""
Example:
// Scala program of multi-line// string literals  // Creating objectobject 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)      }}
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 type Boolean.val x = trueExample:// Scala program of Boolean// literals  // Creating objectobject 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

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

Scala
Scala-Basics
Scala-Data Type
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Scala | flatMap Method
Type Casting in Scala
Scala | yield Keyword
Throw Keyword in Scala
Currying Functions in Scala with Examples
Scala List max() method with example
Scala | Final
Scala List mkString() method with a separator with example
For Loop in Scala
Scala Iterator drop() method with example