Kotlin is a modern programming language used for Android apps, backend services, command-line tools, desktop applications, and multiplatform projects. This Kotlin tutorial is written for beginners who want to learn Kotlin programming step by step, with simple examples and clear explanations.
Kotlin runs on the Java Virtual Machine, works smoothly with Java libraries, and is officially supported for Android development. You can learn Kotlin as your first programming language, or as a practical next language if you already know Java, JavaScript, Python, C#, or another language.
What You Will Learn in This Kotlin Programming Tutorial
This Kotlin tutorial covers the language from basic syntax to object-oriented programming and practical application development concepts. The lessons are arranged so that you can start with simple Kotlin programs and gradually move to functions, classes, inheritance, collections, null safety, exceptions, and file handling.
- Kotlin setup, program structure, and the
main()function - Variables, data types, operators, strings, and input/output
- Control flow using
if,when,for, andwhile - Functions, default arguments, named arguments, and lambdas
- Classes, objects, constructors, inheritance, interfaces, and abstraction
- Null safety, safe calls, Elvis operator, and nullable types
- Arrays, lists, sets, maps, and common collection operations
- Exception handling, packages, visibility modifiers, and basic file handling
- Kotlin for Android development and Kotlin with Java interoperability
Kotlin Language Features Beginners Should Understand First
Kotlin is designed to reduce common programming mistakes while keeping code concise. Before you move into large projects, it helps to understand these core ideas.
- Concise syntax: Kotlin often needs fewer lines than Java for the same task.
- Null safety: Nullable and non-nullable types help prevent many null reference errors.
- Java interoperability: Kotlin can call Java code, and Java code can call Kotlin code in the same project.
- Object-oriented and functional style: Kotlin supports classes, inheritance, interfaces, lambdas, higher-order functions, and extension functions.
- Android support: Kotlin is widely used for Android app development with Android Studio and Jetpack libraries.
How to Set Up Kotlin and Run Your First Program
You can start learning Kotlin in different ways. For small examples, the easiest option is the Kotlin online playground. For Android development, install Android Studio. For command-line Kotlin programs or backend projects, install IntelliJ IDEA or configure the Kotlin compiler with a build tool such as Gradle.
- Kotlin official getting started guide
- Android Developers Kotlin learning resources
- Learn Kotlin for Android
A basic Kotlin program starts from the main() function. The following example prints a message to the output screen.
fun main() {
println("Hello, Kotlin!")
}
Output:
Hello, Kotlin!
Kotlin Syntax Basics with a Simple Example
Kotlin code is usually placed inside functions, classes, and files. Semicolons are optional in most cases. Variables can be declared using val or var. Use val when the value should not be reassigned, and use var when the value may change.
fun main() {
val language = "Kotlin"
var year = 2026
println("I am learning $language in $year")
}
In this example, language is read-only because it is declared with val. The variable year is mutable because it is declared with var. Kotlin can infer the data type from the assigned value, so you do not always need to write the type explicitly.
Kotlin Tutorial Index for Step-by-Step Learning
Use the following Kotlin lessons as a learning path. Start from the basics if you are new to programming, or jump to the required topic if you already know another language.
Kotlin Basics and Program Structure
- Kotlin Hello World Program
- Kotlin Comments
- Kotlin Variables
- Kotlin Data Types
- Kotlin Type Conversion
- Kotlin Operators
- Kotlin Input and Output
Kotlin Strings, Numbers, and Expressions
- Kotlin Strings
- Kotlin String Templates
- Kotlin Numbers
- Kotlin Boolean
- Kotlin Ranges
- Kotlin Null Safety
Kotlin Control Flow Statements
- Kotlin if Expression
- Kotlin when Expression
- Kotlin for Loop
- Kotlin while Loop
- Kotlin do while Loop
- Kotlin break Statement
- Kotlin continue Statement
Kotlin Functions and Lambda Expressions
- Kotlin Functions
- Kotlin Function Parameters
- Kotlin Default Arguments
- Kotlin Named Arguments
- Kotlin Recursion
- Kotlin Lambda Functions
- Kotlin Higher-Order Functions
Kotlin Object-Oriented Programming
- Kotlin Class
- Kotlin Objects
- Kotlin Constructors
- Kotlin Inheritance
- Kotlin Method Overriding
- Kotlin Interfaces
- Kotlin Abstraction
- Kotlin Data Class
- Kotlin Sealed Class
Kotlin Collections and Common Operations
- Kotlin Arrays
- Kotlin List
- Kotlin MutableList
- Kotlin Set
- Kotlin Map
- Kotlin filter()
- Kotlin map()
- Kotlin forEach()
Kotlin Exceptions, Files, and Packages
- Kotlin Exception Handling
- Kotlin try catch
- Kotlin Packages
- Kotlin Visibility Modifiers
- Kotlin Read File
- Kotlin Write File
Kotlin Control Flow Example Using when and for Loop
The when expression is often used when you need to compare one value against multiple possible cases. The for loop is useful for reading values from ranges, arrays, lists, and other collections.
fun main() {
val scores = listOf(82, 91, 67, 74)
for (score in scores) {
val grade = when {
score >= 90 -> "A"
score >= 75 -> "B"
score >= 60 -> "C"
else -> "Needs improvement"
}
println("Score: $score, Grade: $grade")
}
}
Output:
Score: 82, Grade: B
Score: 91, Grade: A
Score: 67, Grade: C
Score: 74, Grade: C
Kotlin Null Safety Example for Safer Code
Null safety is one of the most useful Kotlin features for everyday programming. A regular Kotlin variable cannot hold null unless its type is marked as nullable with a question mark.
fun main() {
val name: String? = "Anu"
val length = name?.length ?: 0
println("Length: $length")
}
The safe call operator ?. reads the length only when name is not null. The Elvis operator ?: provides a fallback value when the nullable expression is null.
Kotlin Class and Object Example for Beginners
Kotlin supports object-oriented programming with classes, constructors, properties, member functions, inheritance, and interfaces. The following example defines a simple class and creates an object from it.
class Student(val name: String, val marks: Int) {
fun result(): String {
return if (marks >= 40) "Pass" else "Fail"
}
}
fun main() {
val student = Student("Rahul", 76)
println("${student.name}: ${student.result()}")
}
Output:
Rahul: Pass
Learning Kotlin for Android Development
If your goal is Android development, learn Kotlin language basics first and then move to Android-specific topics. Android apps use Kotlin with Android Studio, Gradle, Jetpack libraries, XML layouts or Jetpack Compose, and Android lifecycle components.
- Start with Kotlin variables, functions, classes, null safety, and collections.
- Learn Android Studio project structure and Gradle basics.
- Build small screens using Jetpack Compose or XML layouts.
- Understand activities, state, navigation, permissions, and app lifecycle.
- Practice by building small apps such as a calculator, notes app, quiz app, or unit converter.
For Android-specific Kotlin learning, use the official Android Kotlin resources along with the language tutorials on this page.
Kotlin and Java: What Changes for Java Programmers
If you already know Java, Kotlin will feel familiar in many places, but some parts need special attention. Kotlin has properties instead of traditional getter/setter-heavy code, nullable types are explicit, functions can exist outside classes, and data classes reduce boilerplate for simple model objects.
| Kotlin feature | What Java programmers should notice |
|---|---|
val and var | Use val for read-only references and var for mutable references. |
| Nullable types | String and String? are different types in Kotlin. |
| Data classes | Kotlin can generate common model methods such as toString(), equals(), and copy(). |
| Extension functions | You can add function-like behavior to existing types without modifying their source code. |
| Top-level functions | Small utility functions do not need to be placed inside a class. |
Best Way to Learn Kotlin Programming
The best way to learn Kotlin is to write small programs while learning each concept. Avoid reading many topics without practice. Kotlin syntax becomes easier when you regularly test examples, change values, break code intentionally, and understand the compiler errors.
- Run a simple
Hello, Kotlin!program. - Practice
val,var, data types, strings, and operators. - Write programs using
if,when, loops, and ranges. - Create functions with parameters and return values.
- Practice classes, constructors, objects, inheritance, and interfaces.
- Use lists, maps, filters, and loops to solve small data problems.
- Learn null safety by writing examples with nullable values.
- Build a small project such as a student marks calculator, expense tracker, or simple Android screen.
Common Kotlin Mistakes Beginners Should Avoid
- Using
vareverywhere instead of choosingvalfor values that should not be reassigned. - Adding
!!to nullable values without checking whether the value can actually be null. - Trying to write Kotlin exactly like Java and missing Kotlin features such as data classes, safe calls, and top-level functions.
- Ignoring compiler warnings, especially warnings related to unused variables, unsafe casts, and nullable values.
- Learning Android APIs before understanding Kotlin functions, classes, collections, and null safety.
Kotlin Tutorial QA Checklist
- Does the Kotlin example compile with a current Kotlin compiler or in the Kotlin playground?
- Are code blocks marked with the correct
language-kotlinclass, and are output blocks marked asoutput? - Does the tutorial explain when to use
val,var, nullable types, and safe calls? - Are Android-specific statements separated from general Kotlin language explanations?
- Are beginner examples small enough to understand without extra frameworks?
- Are internal Kotlin tutorial links grouped by learning stage rather than listed randomly?
Kotlin Programming FAQs
Is Kotlin easy to learn for beginners?
Kotlin is beginner-friendly because the syntax is concise and readable, but beginners should still learn basic programming ideas such as variables, loops, functions, classes, and errors. It becomes easier when you practice with small programs instead of only reading syntax.
Do I need Java before learning Kotlin?
You do not need Java before learning Kotlin. Kotlin can be learned as a first language. However, Java knowledge is useful when working with older Android projects, Java libraries, or JVM backend applications.
Is Kotlin only for Android app development?
No. Kotlin is widely used for Android, but it can also be used for backend development, command-line programs, desktop applications, scripting, and multiplatform projects.
What should I learn first in Kotlin for Android development?
Start with Kotlin variables, functions, classes, collections, and null safety. After that, learn Android Studio, Gradle basics, Jetpack Compose or XML layouts, app lifecycle, navigation, and state management.
What is the difference between val and var in Kotlin?
val declares a read-only reference that cannot be reassigned after initialization. var declares a mutable reference that can be reassigned. Prefer val unless the value really needs to change.
Conclusion: Start Kotlin with Small, Working Programs
Kotlin is a practical language for Android, JVM, and multiplatform development. Start with the basic Kotlin syntax, write small programs for each concept, and then move to object-oriented programming, collections, null safety, and Android development. Use the tutorial index above as a structured path and practice each lesson with your own examples.
TutorialKart.com