Зміст курсу
Android Development with Kotlin
Android Development with Kotlin
Data Types in Kotlin
Let's talk about data types now. In Kotlin, there are four main data types: numeric, character, boolean, and string.
Let's go through each type:
Integer Data Types
An integer data type represents a number that does not have fractional parts, meaning it's a whole number:
You can see in parentheses after each line the range of values that each data type can accept. From this, we can conclude that the difference between these data types is simply the range of values they can hold.
Naturally, the smaller the value a variable can hold, the less memory it will occupy on the device.
Floating-Point Numeric Data Types
Let's move on to floating-point numeric data types:
As you can see, there are two types. The main difference lies again in the possible number of digits after the decimal point and in the size of such a variable in memory.
Also, pay attention to the letters F
and L
that we used when specifying values for the Float
and Long
data types. These two data types require a letter after determining the value, for example: 10293L
, 9283L
, 12.71F
, and 3.14F
.
You need to accept this fact.
Character Type
In Kotlin, there is only one character data type, which is Char
. This data type holds a single character enclosed in single quotes.
Note
A character can be anything: a letter, a digit, or a symbol. For example: 'A', 'h', '4', '-', '>', 'P', and so on.
Boolean Data Type
Kotlin also has a single boolean data type, which is used for writing and performing logical operations. This data type can take on two values: true
or false
. It's similar to 1
and 0
in discrete mathematics.
This data type is very useful because it allows us to create various execution conditions. You'll see this later and understand the importance of such a data type.
String Data Type
Lastly, but equally important, is the String
data type. As the name suggests, this data type holds strings that should be enclosed in double-quotes.
Note
This data type is more complex than it seems, and we will revisit it to learn more details and work with it thoroughly.
Using and Manipulating Data Types
As I mentioned earlier, variables only store various values, and the data types are determined by the keywords I listed above.
But if we store these values, we must be able to use them, and it's easy!
Let's create a string variable and print to the console value stored in this variable:
main
fun main() { val stringValue: String = "Hello Kotlin!" print(stringValue) }
As you can see, to use a string variable, we simply specify the variable's name and print its value to the console!
Note
We don't need to enclose the variable name in double quotes; we just use the variable name to access its value.
We can also perform various operations with variables, such as mathematical operations with integer values:
Main
fun main() { val numberA: Int = 30 val numberB: Int = 25 var sum: Int = numberA + numberB var mul: Int = numberA * numberB println("The sum is $sum") // if we want to specify the variable value inside some text, we use `$variableName` inside the string println("Multiplication result is $mul") }
As you can see, we can use standard mathematical operations in the code and also use them with variables.
You may have also noticed how we use a variable inside a string. We need to use the special $
sign before the variable's name, and its value will be included within the text.
Let's look at a few more examples of using variable values inside strings:
Main
fun main() { val age: Int = 25 val name: String = "Daniel" val hobby: String = "Programming" println("Hello! My name is $name, I'm $age years old and I like $hobby!") }
We used variables to store potential user information and displayed test information on the console. This way, we can use variables and perform some operations on them.
Let's take a closer look at the mathematical operations we can use with numerical data types.
Basic Mathematical Operations
In Kotlin, the primary operations are:
- Addition operation:
a + b
, resulting in a numeric data type; - Subtraction operation:
a - b
, resulting in a numeric data type; - Multiplication operation:
a * b
, resulting in a numeric data type; - Division operation:
a / b
, resulting in a floating-point numeric data type; - Remainder operation:
a % b
, resulting in a numeric data type.
With these operations, we can perform basic calculations and various computations with variables and numerical values.
Дякуємо за ваш відгук!