Basic Literals in Kotlin

Integer Numbers

  • If an integer value contains a lot of digits, we can add underscores to divide the digits into blocks to make this number more readable: for example, 1_000_000 is much easier to read than 1000000
  • You can add as many underscores as you would like e.g. 1___000_00, 1_2_3
  • Underscores can't appear at the start or at the end of the number. If you write _10 or 100_ you will get an error.

Characters

  • A single character can represent a digit, a letter or another symbol.
  • To write a single character we wrap a symbol in single quotes.
  • A character cannot include two or more digits or letters because it represents a single symbol. e.g. 'abc' and '543' are incorrect character literal.

Strings

  • Strings represent text information. So strings can include letters, digits, whitespaces and other characters.
  • To write strings, we wrap characters in double quotes instead of single ones.
  • A string can also contain just one single character like "A". Do not confuse it with the character 'A' which is not a string.

Do's and Don'ts

  • Underscores can't appear at the start or at the end of an integer number. If you write _10 or 100_ you will get an error.
  • 123 is an integer and "123" is a string.
  • 'A' is a character and "A" is a string.
  • '1' is a character and 1 is an integer number.
  • A character cannot include two or more digits or letters because it represents a single symbol. e.g. 'abc' and '543' are incorrect character literal.


Comments