DEV Community

Cover image for Identifiers in Kotlin
Jay Tillu😎
Jay Tillu😎

Posted on • Updated on

Identifiers in Kotlin

  • Identifiers are the name given to objects, variables, classes, and functions, etc. For example: var age = 45

  • Here var is a keyword and age is the name given to variable.

Rules for naming identifiers


There are five rules which are applied to naming identifiers.

  • Identifiers can contain letters, underscore, and digits. But must not start with a digit.
var 1userBalance = 2500  // Identifier cannot start with numbers.
var $dollar = 60  // Identifiers cannot contain special symbols.
Enter fullscreen mode Exit fullscreen mode
  • Whitespaces are not allowed.
var High Score  // Identifiers cannot contain whitespaces.
Enter fullscreen mode Exit fullscreen mode
  • Special symbols are not allowed except underscore.
($, #, ?, <>, ~, ! Not allowed)
Enter fullscreen mode Exit fullscreen mode
  • Identifiers are case sensitive.
var HighScore or highScore // both highscores are different here.
Enter fullscreen mode Exit fullscreen mode
  • In kotlin, a hard keyword cannot be used as identifiers.
var class  // hard keywords cannot be used as identifiers.
Enter fullscreen mode Exit fullscreen mode
  • When creating variables, classes, objects or functions, choose a name that makes sense. For example score, number, levels, userData makes more sense then variable name such as a, b, c. Although they are valid.

Camel Casing

  • Camel casing is a naming pattern, mostly used in naming programming.

  • There are two types of camel casing:

  • Upper camel case

  • Lower camel case

  • Use camel casing naming method is always recommended in programming.

  • Upper camel case — In upper camel case, the first letter of the new word is upper case, allowing it to be easily distinguished from a lower case.

var HighScore = 250   //Example of upper camel case.
Enter fullscreen mode Exit fullscreen mode
  • Lower camel case — In Lower camel case, the first letter of the first name is the lower case then after every new word will be upper case, allowing it to easily distinguished from an upper case.
var highScore = 250   //Example of lower camel case.
Enter fullscreen mode Exit fullscreen mode

What is WhiteSpaces?


  • In computer programming, white space is any character or series of character that represents horizontal or vertical space in typography.
var high Score = 250   //Compile time error. Whitespace not allowed.
Enter fullscreen mode Exit fullscreen mode
  • Space bar, tab and enter button mostly used to insert whitespaces.

  • Remember most of the compilers don’t allow whitespaces. So avoid them as much as possible in your programs.

Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

Latest comments (0)