Various kinds of data types in Kotlin and its utility
Kotlin is a modern programming language that compiles to Java bytecode. It is free and open source and promises to make coding for Android even more fun. Kotlin is interoperable with Java. In other words, it can be used together with Java in the same project. So you can refactor parts of your Java code to Kotlin and it won't break. In addition to that, it is concise, expressive, and has great tooling. Kotlin can be used on the back-end (server-side), but it's getting a lot of attention right now as a language for Android app development. Kotlin is now supported by Google as a first-class language for Android development.
In this post, we describe the data types used in Kotlin.
In Kotlin, data type (basic type) refers to type and size of data associated with variables and functions. The data type is used for declaration of the memory location of a variable which determines the features of data.
Kotlin built-in data types are categorized as follows -
Kotlin handles numbers in a way close to Java, but not exactly the same. For example, there are no implicit widening conversions for numbers, and literals are slightly different in some cases. Numbers in Kotlin are similar to Java. There are 6 built-in types representing numbers -
Byte - The Byte data type can have values from -128 to 127 (8-bit signed two's complement integer). It's used instead of Int or other integer data types to save memory if it's certain that the value of a variable will be within [-128, 127].
Short - The Short data type can have values from -32768 to 32767 (16-bit signed two's complement integer). It's used instead of other integer data types to save memory if it's certain that the value of the variable will be within [-32768, 32767].
Int - A Int data type can have values from -231 to 231-1 (32-bit signed two's complement integer). If you assign an integer between -231 to 231-1 to a variable without explicitly specifying its type, the variable will be of Int type.
Long - The Long data type can have values from -263 to 263-1 (64-bit signed two's complement integer). If you assign an integer value greater than 231-1 or less than -231 to a variable (without explicitly specifying its type), the variable will be of a Long type.
Float - The Float data type is a single-precision 32-bit floating point. Learn more about single precision and double precision floating point if you are interested. If you are not sure what number value a variable will be assigned in the program, you can specify it as Number type. This allows you to assign both integer and floating-point value to the variable (one at a time).
Double - The Double type is a double-precision 64-bit floating point.
To represent a character in Kotlin, Char types are used. Unlike Java, Char types cannot be treated as numbers. Character variable cannot be declared like number variables. Kotlin variable can be declared in two ways - one using “var” and another using “Val”.
The type Boolean is used to represent logical values. It can have two possible values true and false. Booleans are boxed if a nullable reference is needed. Booleans are used in decision-making statements.
Strings are character arrays. Like Java, they are immutable in nature. There have two kinds of string available in Kotlin - one is called raw String and another is called escaped String. raw String can be created with triple quotes. And escaped String can be used with double quotes. Kotlin also supports string interpolation or string templates. This is an easier way to build dynamic strings than concatenation, which is what we use in Java. Using string templates, we can insert variables and expressions into a string.
Arrays are a collection of homogeneous data. Like Java, Kotlin supports arrays of different data types. there are two main ways to create an array: using the helper function arrayOf() or the constructor Array().
Ranges is another unique characteristic of Kotlin. Like Haskell, it provides an operator that helps you iterate through a range. Internally, it is implemented using rangeTo() and its operator form is (..).
The collection is a very important part of the data structure. Kotlin has two types of the collection - one is an immutable collection (which means lists, maps, and sets that cannot be editable) and another is a mutable collection (this type of collection is editable).
Photograph by fatmawati achmad zaenuri