Regardless of their complexity, all programs essentially perform operations on numbers, strings, and other values. These values are called literals i.e. in the most basic sense or meaning of the symbol.
Before we start writing our first programs, let’s learn the basic literals in Kotlin: integer numbers, characters, and strings. You can meet these literals everywhere in everyday life.
We use integer numbers to count things in the real world. We will also often use integer numbers in Kotlin.
Here are several examples of valid integer number literals separated by commas:
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
You can add as many underscores as you would like:
1_2_3. Remember, underscores can’t appear at the start or at the end of the number. If you write
100_ , you get an error.
A single character can represent a digit, a letter, or another symbol. To write a single character, we wrap a symbol in single quotes as follows:
'9'. Character literals can represent alphabet letters, digits from
'9', whitespaces (
' '), or some other symbols (e.g.,
Do not confuse characters representing numbers (e.g.,
'9') and numbers themselves (e.g.,
A character cannot include two or more digits or letters because it represents a single symbol. The following two examples are incorrect:
'543' because these literals have too many characters.
Strings represent text information, such as the text of an advertisement, the address of a web page, or the login to a website. A string is a sequence of any individual characters.
To write strings, we wrap characters in double quotes instead of single ones. Here are some valid examples:
"I want to learn Kotlin",
"email@example.com". So, strings can include letters, digits, whitespaces, and other characters.
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 not confuse these literals:
123is an integer number, and
"123"is a string;
'A'is a character, and
"A"is a string;
'1'is a character, and
1is an integer number.