A data type is an attribute of data which tells the compiler or interpreter what type of value the data contains. Based of the illustration variables as boxes before, we can think of the data types as color of the paper, each color can only hold one type of data (yellow for strings, blue for integers, and green for booleans).

Variables as boxes — Box Vector by pch.vector on Freepik
Basic data types

But what are strings, integers, or booleans? They are all the basic data types that Kotlin (and most programming languages) already has.

In this section we describe the basic types used in Kotlin: numbers, characters, booleans, arrays, and strings.

  • Numbers. Kotlin provides a set of built-in data types that represent numbers. For integer/whole numbers, there are four types with different sizes and value ranges. Kotlin provides types Float and Double for floating-point/fractional numbers.
Data types for integer numbers — kotlinlang.org
Data types for fractional numbers — kotlinlang.org
Example of number-typed variables
  • Characters. Represented by the data type Char for single character value.
Example of a Char variable
  • Booleans. Represented by the data type Boolean for one of the two possible values: true and false.
Example of Boolean variables
  • Arrays. Represented by the Array class for group of objects.
Example of Array variables
  • Strings. Represented by the data type String for group of characters (text).
Example of String variable

Type inference is a concept that allows you to omit some information — like data type — that Kotlin compiler can infer. We can ommit the data type when we declare a variable if we directly assign the intial value.

Example of type inferenced variables

Woah! We have already covered the topics of variables and data types. Today we learned what is data types and basic data types in Kotlin. I’m happy that you make it till the end of this article, really appreciate it. As always feel free to drop a comment, and I will see you in the next one.

— Learn Kotlin Series —

  1. Learn Kotlin: Introduction
  2. Learn Kotlin: Hello World!
  3. Learn Kotlin: Variables
  4. Learn Kotlin: Data Types (this article)
  5. Learn Kotlin: Basic Operators
  6. Learn Kotlin: Functions

Android Developer. Currently residing in Jambi, Indonesia. https://romnan.my.id