Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM). It was developed by JetBrains and is now an official language for Android development. Kotlin is designed to be concise, expressive, and safe. It is also interoperable with Java, which means that you can use Kotlin and Java code together in the same project.
Kotlin Data Type (Lesson 4 PDF)
Overview about the Lesson
In this lesson, you will learn about data types in Kotlin. You will learn about the basic data types such as numbers, characters, and booleans. You will also learn about arrays, strings, and nullability.






The Content of the Lesson
The lesson is divided into two parts. In the first part, you will learn about the basic data types in Kotlin. You will learn about numbers, characters, and booleans. You will also learn about type inference and how to declare variables and constants.
In the second part, you will learn about arrays, strings, and nullability. You will learn how to declare arrays, how to work with strings, and how to handle null values.
Why This Lesson
This lesson is designed for programmers who are new to Kotlin and want to learn about data types. It is also suitable for programmers who are familiar with Java and want to learn Kotlin.
Kotlin Data Types at a Glance
The PDF explains each data type in detail. This reference summarises them with examples and shows the conversions beginners get stuck on.
Number types
| Type | Size | Range / precision | Example |
|---|---|---|---|
| Byte | 8 bits | −128 to 127 | val b: Byte = 100 |
| Short | 16 bits | −32,768 to 32,767 | val s: Short = 2000 |
| Int | 32 bits | about ±2.1 billion | val i = 42 |
| Long | 64 bits | about ±9.2 × 1018 | val l = 42L |
| Float | 32 bits | about 6–7 significant digits | val f = 3.14f |
| Double | 64 bits | about 15–16 significant digits | val d = 3.14 |
Whole numbers default to Int (or Long if too big for Int); decimals default to Double. Use underscores for readability: val population = 106_000_000.
Other basic types
fun main() {
val isActive: Boolean = true
val grade: Char = 'A' // single quotes for Char
val message: String = "Hello" // double quotes for String
println("$isActive $grade ${message.length}") // true A 5
}Type conversion
Kotlin never converts number types silently, which prevents subtle bugs. Convert explicitly:
fun main() {
val count: Int = 7
val total: Long = count.toLong()
val ratio: Double = count.toDouble() / 2 // 3.5
val text = "123"
val number = text.toInt() // 123
val safe = "12a".toIntOrNull() // null instead of a crash
println("$total $ratio $number $safe") // 7 3.5 123 null
}Use toIntOrNull() whenever the text comes from a user or a file, because toInt() throws an exception on invalid input.
Nullable types
var email: String? = null // may be null
println(email?.length) // safe call: prints null
println(email ?: "no email") // Elvis operator: fallback valueA type without ? can never hold null. This is one of Kotlin’s most important safety features.
Checking a value’s type
fun describe(x: Any): String =
if (x is String) "Text of length ${x.length}" else "Not text"After x is String, Kotlin automatically treats x as a String inside that branch (a smart cast).
Practice exercise
Read the string “45” as a number, add 5 and print the result as a Double.
fun main() {
val input = "45"
val result = (input.toInt() + 5).toDouble()
println(result) // 50.0
}Frequently Asked Questions
Does Kotlin have primitive types like Java?
Everything looks like an object in Kotlin code, but the compiler uses JVM primitives (int, double…) under the hood whenever it can, so there is no performance penalty.
Why does 0.1 + 0.2 not print exactly 0.3?
Double stores numbers in binary, so some decimals cannot be represented exactly. For money, use BigDecimal or work in whole cents with Long.
Continue the course
← Previous: Lesson 3: Comments & Variables | Next: Lesson 5: Operators →
- Lesson 1: Getting Started
- Lesson 2: Syntax & Output
- Lesson 3: Comments & Variables
- Lesson 4: Data Types (you are here)
- Lesson 5: Operators
- Lesson 6: Set Operations & Grouping
- Lesson 7: Booleans & If…Else
- Lesson 8: When, While, Break & Continue
- Lesson 9: Arrays
- Lesson 10: For Loops & Ranges
- Lesson 11: Functions & Classes
- Lesson 12: Classes, Objects & Constructors
- Lesson 13: Class Functions & Inheritance
Conclusion
Data types are an essential part of any programming language. In this lesson, you learned about the basic data types in Kotlin such as numbers, characters, and booleans. You also learned about arrays, strings, and nullability. With these skills, you can write more efficient and maintainable code.
Download From Link
You can download the lesson from the following link:














