Scala Identifiers

In programming languages, Identifiers are used for identification purpose. In Scala, an identifier can be a class name, method name, variable name or an object name.
For example :

class GFG{
    var a: Int = 20
}
object Main {
    def main(args: Array[String]) {
        var ob = new GFG();
    }
}

In the above program we have 6 idtentifiers:

  • GFG: Class name
  • a: Variable name
  • Main: Object name
  • main: Method name
  • args: Variable name
  • ob: Object name

Rules for defining Java Scala

There are certain rules for defining a valid Scala identifier. These rules must be followed, otherwise we get a compile-time error.



  • Scala identifiers are case-sensitive.
  • Scala does not allows you to use keyword as an identifier.
  • Reserved Words can’t be used as an identifier like $ etc.
  • Scala only allowed those identifiers which are created using below four types of identifiers.
  • There is no limit on the length of the identifier, but it is advisable to use an optimum length of 4 – 15 letters only.
  • Identifiers should not start with digits([0-9]). For example “123geeks” is a not a valid Scala identifier.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to demonstrate
// Identifiers
  
object Main 
{
      
    // Main method
    def main(args: Array[String])
    {
          
    // Valid Identifiers
    var `name` = "Siya";
    var _age = 20;
    var Branch = "Computer Science";
    println("Name:" +`name`);
    println("Age:" +_age);
    println("Branch:" +Branch);
    }
}

chevron_right


Output:

Name:Siya
Age:20
Branch:Computer Science

In the above example, valid identifers are:

Main, main, args, `name`, _age, Branch, +

and keywords are:

Object, def, var, println

Types of Scala identifers

Scala supports four types of identifiers:

  1. Alphanumeric Identifiers: These identifiers are those identifiers which start with a letter(capital or small letter) or an underscore and followed by letters, digits, or underscores.

    Example of valid alphanumeric identifiers:

     _GFG, geeks123, _1_Gee_23, Geeks

    Example of Invalid alphanumeric identifiers:

    123G, $Geeks, -geeks

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program to demonstrate
    // Alphanumeric Identifiers
      
    object Main 
    {
          
        // Main method
        def main(args: Array[String]) 
        {
              
        // main, _name1, and Tuto_rial are 
        // valid alphanumeric identifiers
        var _name1: String = "GeeksforGeeks"
        var Tuto_rial: String = "Scala"
          
        println(_name1);
        println(Tuto_rial);
        }
    }

    chevron_right

    
    

    Output:

    GeeksforGeeks
    Scala
    
  2. Operator Identifiers: These are those identifiers which contain one or more operator character like +, :, ?, ~, or # etc.
    Example of vaild operator identifiers:



     +, ++ 

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program to demonstrate
    // Operator Identifiers
      
    object Main 
    {
          
        // Main method
        def main(args: Array[String])
        {
              
        // main, x, y, and sum are valid 
        // alphanumeric identifiers
        var x:Int = 20;
        var y:Int = 10;
          
        // Here, + is a operator identifer 
        // which is used to add two values
        var sum = x + y; 
        println("Display the result of + identifier:");
        println(sum);
        }
    }

    chevron_right

    
    

    Output:

    Display the result of + identifier:
    30
    
  3. Mixed Identifiers: These are those identifiers which contains alphanumeric identifiers followed by undersoce and an operator identifier.
    Example of vaild mixed identifiers:

     unary_+, sum_= 

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program to demonstrate
    // Mixed Identifiers
      
    object Main 
    {
          
        // Main method
        def main(args: Array[String]) 
        {
              
        // num_+ is a valid mixed identifier
        var num_+ = 20;
        println("Display the result of mixed identifier:");
        println(num_+);
        }
    }

    chevron_right

    
    

    Output:

    Display the result of mixed identifier:
    20
    
  4. Literal Identifiers: These are those identifers in which an arbitrary string enclosed with back ticks (`….`) .
    Example of vaild mixed identifiers:

    `Geeks`, `name` 

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program to demonstrate
    // Literal Identifiers
      
    object Main 
    {
          
        // Main method
        def main(args: Array[String]) 
        {
              
        // `name` and `age` are valid literal identifiers
        var `name` = "Siya"
        var `age` = 20
        println("Name:" +`name`);
        println("Age:" +`age`);
        }
    }

    chevron_right

    
    

    Output:

    Name:Siya
    Age:20
    


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.