Kotlin is a statically typed programming language, which means that all variables and expressions are checked for their types during compilation (translating the written source code into executable machine code that can be understood by the computer). If one tries to assign a value to a data type that does not match the expected type, an error will occur and the program will stop compiling. This has the advantage that certain errors can be identified at an early stage of the translation process. Therefore, it is important to learn how to use data types in Kotlin from the beginning to ensure that our variables and expressions are of the correct type and that our code works correctly.

All parts of a program in Kotlin, including variables, represent objects with a specific data type. The data type determines which one storage space requirements has an object of this type and which one operations can be carried out with it. Kotlin provides various basic data types such as numbers, characters, strings, booleans, and arrays.

Now take a closer look at these data types.

Integer data types

The byte data type is used to store integers in the range -128 to 127. It takes up 8 bits (1 byte) of storage space.

Shorts: The Short data type is used to store integers in the range -32.768 to 32.767. It takes up 16 bits (2 bytes) of storage space.

Int: The Int data type is used to store integers in the range -2.147.483.648 to 2.147.483.647. It takes up 32 bits (4 bytes) of storage space.

Long: The Long data type is used to store large integers in the range -9.223.372.036.854.775.808 to 9.223.372.036.854.775.807. It takes up 64 bits (8 bytes) of storage space.

Long: The Long data type is used to store large integers in the range -9.223.372.036.854.775.808 to 9.223.372.036.854.775.807. It takes up 64 bits (8 bytes) of storage space.

Unsigned integer data types

Starting with version 1.3 of Kotlin, support for unsigned integer data types added. Compared to signed integer data types, they offer several advantages such as a wider range of values, higher efficiency, and improved code readability. They are also important to ensure correct communication with other systems.

UByte: stores integers in the range 0 to 255 and takes up 1 byte of storage space. 

UShort: stores integers in the range 0 to 65.535 and takes up 2 bytes of storage space.

UInt: stores integers in the range 0 to 4.294.967.295 and takes up 4 bytes of storage space.

ULong: stores integers in the range 0 to 18.446.744.073.709.551.615 and takes up 8 bytes of storage space.

floating point numbers

Floating point numbers are numbers that can contain decimal places. In Kotlin they are used with the data type Float or Double declared. 

Float stores a floating point number in the range -3.410^38 to 3.410^38 and requires 4 bytes. A variable of type Float is assigned a number by using the suffix f or F after the number.

Double stores a floating point number in the range ±5.010^-324 to ±1.710^308 and requires 8 bytes.

The Double data type also supports exponential notation:

Boolean

Boolean is a logical data type that can take two possible values ​​in Kotlin: true (true) or false (incorrect). It is used to represent a statement or condition that is either true or false.

Char

Char is a data type in Kotlin that represents individual characters. Each char value is a Unicode character and is enclosed in single quotation marks (“”). A char value requires 2 bytes of storage space and can represent all possible Unicode characters.

The Char data type can also represent special escape sequences that are interpreted in a special way:

  • \n: new line
  • \r: carriage return
  • \': single quotation mark
  • \t: tab
  • \": double quotation mark
  • \\: backslash

String

The data type String in Kotlin represents a string of characters that can be interpreted as text. In Kotlin, a string is enclosed in double or triple quotes (““…”” or “““…”””).

The String data type allows you to create character strings with special characters or escape sequences mentioned above. For example, if you want to insert a line break into the text, you can use the escape sequence \n use.

To make it easier to create multi-line text, triple quotation marks can also be used.

String templates

With the help of String templates Variable values ​​can be easily inserted into a character string. The dollar sign is used for this $ used to access the variable value and insert it into the string.

In this example the values ​​of the variables $websiteName, $launchYear and $numberOfArticles are embedded into the string. As one can see, variables have the data types String and Int.

Type inference (type derivation)

Kotlin uses type inference to automatically determine the type of a variable based on the data with which it is initialized. For this reason, the type can be omitted when initializing the variable.

IMPORTANT: It is not allowed to first create a variable without a type declaration and later assign a value to it.

Static typing and data type Any

The data type of a variable indicates which values ​​can be assigned to that variable. Attempting to assign a different data type results in an error.

In Kotlin there is also the special data type Any, which allows assigning any value to any variable.

“KotlinTutorial.de” In Kotlin there are other data types such as arrays, lists, maps, classes and more. These will be covered in detail later in the tutorial. And at this point we offer you a selection of exercises to consolidate the content of this chapter.


Leave a Comment

Your e-mail address will not be published. Required fields are marked with * marked