Kotlin is a modern programming language. It runs on the Java Virtual Machine and can create applications for many platforms. This article provides a basic introduction to the language. It focuses on core concepts with simple examples. You will learn enough to start writing basic Kotlin programs.
Setting Up Your Environment
Before you begin, you need a Kotlin development environment. You can use IntelliJ IDEA which offers excellent Kotlin support. Alternatively, you can use the Kotlin Playground, an online environment for quick testing. The Playground requires no installation.
Variables and Data Types
Variables store data. Kotlin uses type inference. This means you often do not need to explicitly declare a variable’s type. The compiler determines it from the assigned value.
val name = "Alice" // String
val age = 30 // Int
val price = 9.99 // Double
val isEmployed = true // Boolean
val declares a read-only variable. Use var to declare a variable you can change.
var score = 0
score = 10
Kotlin provides several built-in data types. These include Int, Double, Boolean, String, and Char.
Operators
Kotlin supports standard arithmetic operators. These include +, -, *, /, and %. It also supports comparison operators like ==, !=, >, <, >=, and <=. Logical operators include && (and), || (or), and ! (not).
val x = 10
val y = 5
val sum = x + y
val greater = x > y
val bothTrue = true && true
Control Flow
Control flow statements control the order in which code executes.
If Statements
if statements execute code based on a condition.
val number = 7
if (number > 5) {
println("Number is greater than 5")
}
You can add an else block for alternative execution.
val number = 3
if (number > 5) {
println("Number is greater than 5")
} else {
println("Number is 5 or less")
}
When Statements
when statements provide a concise way to handle multiple conditions. They are similar to switch statements in other languages.
val day = 3
when (day) {
1 -> println("Monday")
2 -> println("Tuesday")
3 -> println("Wednesday")
else -> println("Another day")
}
Loops
Loops repeat a block of code.
For Loops
for loops iterate over a range or collection.
for (i in 1..5) {
println(i)
}
This loop prints numbers from 1 to 5.
While Loops
while loops continue executing as long as a condition is true.
var count = 0
while (count < 3) {
println(count)
count++
}
Functions
Functions group reusable code. They accept input and can return output.
fun greet(name: String): String {
return "Hello, $name!"
}
val message = greet("Bob")
println(message)
This function takes a String as input and returns a greeting message.
Basic Data Structures
Kotlin provides built-in data structures.
Lists
Lists store collections of items.
val numbers = listOf(1, 2, 3, 4, 5)
println(numbers[0]) // Accessing the first element
Maps
Maps store key-value pairs.
val ages = mapOf("Alice" to 30, "Bob" to 25)
println(ages["Alice"]) // Accessing the age of Alice
Conclusion
This article covered the fundamental concepts of Kotlin. You now have a basic understanding of variables, operators, control flow, functions, and data structures. Continue practicing and exploring the language to build more complex applications. The official Kotlin documentation provides comprehensive information.