Various kinds of data types in Kotlin and its utility

Various kinds of data types in Kotlin and its utility

January 24, 2019
4 min read
1017 views
0

Various kinds of data types in Kotlin

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 -

Numbers

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.

Characters

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”.

Booleans

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

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

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

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 (..).

Collections

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

Was this article helpful?Vote to let the author know
0
Share this article

Loading comments...

Related Articles

Why Proactive Technology Management Matters for Growing Businesses

Why Proactive Technology Management Matters for Growing Businesses

Proactive technology management helps businesses prepare for these demands rather than simply responding when something goes wrong. By regularly reviewing systems, identifying risks and planning improvements, growing companies can build a stronger technology foundation for the future.

5 views
Read More
Read full article: Why Proactive Technology Management Matters for Growing Businesses
Free AI Tools Every Developer Should Try in 2026

Free AI Tools Every Developer Should Try in 2026

The good news is that developers are better served by genuinely free options than almost any other audience. This article covers the categories where free tools are actually usable, and what to watch out for.

6 views
Read More
Read full article: Free AI Tools Every Developer Should Try in 2026
The Weld Decides The Rating

The Weld Decides The Rating

The component rating covers the component. What determines how much the installed anchor can actually take is the weld holding it there and the structure underneath it, and neither of those came from the manufacturer

63 views
Read More
Read full article: The Weld Decides The Rating