Labels

Monday, April 11, 2022

Scala Identifiers (Class -28)

Identifiers in a programming language are the names given to a class, method, variable or object to identify them in the program.

  e.g.:- myObjectmainargsvalue1, etc.

Rules to declare an identifier:

a)      You cannot use a Scala keyword as identifier in Scala. E.g., class is not a valid identifier.

b)     Reserved words cannot be used as identifiers in Scala. E.g., $ cannot be an identifier.

c)      There is no upper limit to the number of characters used in the identifier. “WelcomeToScalaPL” is also a valid identifier.

d)     An identifier cannot start with digits. Example, 3821Scala is not a valid identifier.

e)      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:

  1. Alphanumeric Identifiers
  2. Operator Identifiers
  3. Mixed Identifiers
  4. Literal Identifiers

1) Alphanumeric Identifier

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.

e.g.: scala456, _value1, value_2, etc.

2) Operator Identifier

This type of identifier contains only operators i.e., they are a combination of one or more operators in Scala. The operators include '+', '#', '*', '<='.

3) Mixed Identifier

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.  E.g.:-  val_+, avg_=, etc.

4) Literal Identifier

A string literal used as an identifier in Scala is a literal identifier. These are strings enclosed inside ticks ('...').  e.g.:- ‘value’, ‘result’, etc.

No comments:

Post a Comment