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 (-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
- 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 Integers literals are specified in two ways:Example:
// Scala program of integer// literals// Creating objectobjectinteger{// Main methoddefmain(args:Array[String]){// decimal-form literalvala=46// Hexa-decimal form literalvalb=0xfF// Displays results in// integer formprintln(a)println(b)}}chevron_rightfilter_noneOutput: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.14159
Example:
// Scala program of floating// point literals// Creating objectobjectdouble{// Main methoddefmain(args:Array[String]){// decimal-form literalvala=3.156// It is also a decimal// form of the literalvalb=0123.34// Displays resultsprintln(a)println(b)}}chevron_rightfilter_noneOutput: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 program of character// literal// Creating objectobjectliteral{// Main methoddefmain(args:Array[String]){// Creating a character literal// in single quotevalx='b'// uni-code representation of// character literalvaly='\u0051'// Escape sequence in character// literalsvalz='\n'// Displays resultsprintln(x)println(y)println(z)}}chevron_rightfilter_noneOutput:
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 objectobjectliteral{// Main methoddefmain(args:Array[String]){// Creating a string// literalvalx="GeeksforGeeks"// Displays string// literalsprintln(x)}}chevron_rightfilter_noneOutput: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 objectobjectliteral{// Main methoddefmain(args:Array[String]){// Creating a multiple// line string literalvalx="""GeeksforGeeksis acomputer scienceportal"""// Displays multiple// linesprintln(x)}}chevron_rightfilter_noneOutput: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 = true
Example:
// Scala program of Boolean// literals// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){// Assigning truevala=true// Assigning falsevalb=false// Displays resultsprintln(a)println(b)}}chevron_rightfilter_noneOutput:true false

