Conversion between numeric types There are three most common numeric types: Int, Long, and Double. Sometimes, you may need to assign a value of one numeric type to a variable of another numeric type. In this case, you need to carry out type conversion by invoking a special function, for example, toInt(), toLong(), toDouble(), and so on.
The classification of basic types
In this topic, we will learn about the classification and properties of basic types in Kotlin. You may know some of them. Basic types can be separated into several groups according to their meaning. The types from the same group operate similarly, but they have different sizes and, as a consequence, represent different ranges of values.
What is a data type? All of us know that a number and a piece of text are pretty different. How do we know this? Well, you can perform arithmetic operations (such as multiplication) on numbers but not on texts. Kotlin also knows it. That’s why every variable has a type that determines what possible operations you can perform on this variable and which values you can…
You already know that objects are really complex structures and variables only point to objects. This time, you will learn about equality and how to understand that variables point to the same object. In addition, you will finally fully understand the meaning of the val keyword and avoid one of the most common beginner mistakes: assuming that the val keyword guarantees immutability.
Everything is an object In our life, we are always surrounded by objects: you drive a car, watch TV, read books, pay for bananas with your smartphone. In Kotlin, every time when you work with variables, you work with objects. For example, an integer 5 and a string “love” are objects. It is convenient because programmers are people, and people are used to dealing with objects.…
Sometimes, you need to use a variable that should not be modified during the program execution. Such variables are known as constants, or val variables in Kotlin, where they are declared with the val keyword. The difference between a var variable and a val variable is that we cannot modify the value of a val variable once assigned.
Values and variables
What is a variable? A variable is a storage for a value, which can be a string, a number, or something else. Every variable has a name (or an identifier) to distinguish it from other variables. You can access a value by the name of the variable.
Kotlin String templates are a powerful tool for formatting strings. However, they have limitations, especially when one wants to print numerical values. Here, the String format() member function comes into play, which provides much more flexibility.
Reading data with a readln
In this topic, you will learn how we can read information from the user input and interact with it. Quite often, programs prompt the user for some specific information. We will also see how to manage variables with the function readln() and work with different types.
Standard input is a stream of data that is fed to a program. By default, standard input gets data from the keyboard, but it is also possible to get it from a file.