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

  1. 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 = 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

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program of integer
    // literals
      
    // Creating object
    object 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)
        }
    }

    chevron_right

    
    

    Output:

    46
    255
    

    Note: The Octal form of the literal is obsolete.

  2. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)
        }
    }

    chevron_right

    
    

    Output:

    3.156
    123.34
    

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

  3. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)
        }
    }

    chevron_right

    
    

    Output:

    b
    Q
    

    The character literals are enclosed in a single quote.

  4. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)
        }
    }

    chevron_right

    
    

    Output:

    GeeksforGeeks
    

    A single line string literals are enclosed in a quotation marks.

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

    val x = """GfG"""

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)
      
        }
    }

    chevron_right

    
    

    Output:

    GeeksforGeeks
    is a
    computer science
    portal
    

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

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

    val x = true

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)
        }
    }

    chevron_right

    
    

    Output:

    true
    false
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.