Home »
Scala
Scala Identifiers
By IncludeHelp Last updated : October 07, 2024
What are Identifiers?
Identifiers in a programming language are the names given to a class, method, variable or object to identify them in the program.
Example
Here is a sample Scala code:
object myObject {
def main(args: Array[String]) {
var value1 = 54
var value2 = 65
println("The sum of two values is " + (value1 + value2))
}
}
Output
The sum of two values is 119
All identifiers in this program are, myObject, main, args, value1, value2.
The declaration of these identifiers is based on certain rules. And programs violating these rules will return a compiler-Time error on compilation.
Rules to declare an identifier
- You cannot use a Scala keyword as identifier in Scala. Example, class is not a valid identifier.
- Reserved words cannot be used as identifiers in Scala. Example, $ cannot be an identifier.
- There is no upper limit to the number of characters used in the identifier. WelcomeToScalaProgrammingLanguage is also a valid identifier.
- An identifier cannot start with digits. Example, 2421Scala is not a valid identifier.
- Scala identifiers are case-sensitive. Example, isgreater and isGreater are different identifiers.
Types of identifiers
In Scala, we have 4 different types of identifiers defined. They are:
- Alphanumeric Identifiers
- Operator Identifiers
- Mixed Identifiers
- Literal Identifiers
1) Alphanumeric Identifiers
As the name suggests, these identifiers are named using alphabets and numbers and underscore. As an identifier cannot start with a number, the start of an alphanumeric identifier is underscore (_) or a character followed by a number.
Example
Scala123, is12thDigit
Scala Example of Alphanumeric Identifiers
object myObject {
def main(args: Array[String]) {
var _value1 = 54
var value_2 = 65
println("The sum of two values is " + (_value1 + value_2))
}
}
Output
The sum of two values is 119
2) Operator Identifiers
This type of identifier contains only operators i.e., they are a combination of one or more operators in Scala. The operators include '+', '#', '*', '<='.
Example
=== , +
Scala Example of Operator Identifiers
object myObject {
def main(args: Array[String]) {
var _value1 = 54
var value_2 = 65
println("The result after using '+' identifier is " + (_value1 + value_2))
}
}
Output
The result after using '+' identifier is 119
3) Mixed Identifiers
As the name suggests, these types of identifiers are a mixture of the above stated two types of identifiers. it contains an alphanumeric identifier followed by an operator identifier with an underscore in between.
Example
avg_+, val_=
Scala Example of Mixed Identifiers
object myObject {
def main(args: Array[String]) {
var _value1 = 54
var value_2 = 65
var val_+ = _value1 + value_2
println("The sum is " + (val_+))
}
}
Output
The sum is 119
4) Literal Identifiers
A string literal used as an identifier in Scala is a literal identifier. These are strings enclosed inside ticks ('...').
Example
`scala`, `value`
Scala Example of Literal Identifiers
object myObject {
def main(args: Array[String]) {
var `firstValue` = 54
var `secondValue` = 65
var `result` = `firstValue` + `secondValue`
println("The result of sum is " + (`result`))
}
}
Output
The result of sum is 119