Kotlin is a statically typed programming language developed by JetBrains and officially supported by Google for Android app development. It runs on the Java Virtual Machine (JVM) and also compiles to JavaScript source code or LLVM bitcode, making it versatile and suitable for various platforms. Kotlin's design philosophy emphasizes safety, readability, and interoperability with Java.
In this section, we will explore the basics of Kotlin, including its syntax, data types, variables, control structures, functions, and object-oriented features. By the end of this tutorial, you should have a solid understanding of Kotlin fundamentals and be ready to start building applications in Kotlin.
Before diving into Kotlin, ensure that your development environment is set up correctly. You will need:
File > Settings (or Preferences on macOS).Plugins.Kotlin's syntax is clean and concise, making it easy to read and write. Here are some basic examples:
fun main() {
println("Hello, Kotlin!")
}
fun keyword defines a function.main() is the entry point of a Kotlin application.Kotlin supports both mutable (var) and immutable (val) variables. Immutable variables are constants once assigned.
// Mutable variable
var age: Int = 25
age = 30
// Immutable variable
val name: String = "John"
Kotlin can infer types from the context, reducing boilerplate code.
val message = "Hello" // Kotlin infers type as String
var count = 10 // Kotlin infers type as Int
Kotlin provides familiar control structures like if, when, loops (for and while), and more.
val number = 5
if (number > 0) {
println("Positive")
} else if (number < 0) {
println("Negative")
} else {
println("Zero")
}
Kotlin's when is a powerful alternative to multiple if-else statements.
val day = "Monday"
when (day) {
"Monday" -> println("Start of the week")
"Friday" -> println("End of the week")
else -> println("Weekday")
}
for (i in 1..5) {
println(i)
}
var i = 0
while (i < 5) {
println(i)
i++
}
Functions are first-class citizens in Kotlin, allowing you to pass them as arguments and return them from other functions.
fun greet(name: String): String {
return "Hello, $name!"
}
println(greet("Alice"))
For functions with a single expression, you can use the shorthand syntax.
fun add(a: Int, b: Int) = a + b
println(add(3, 4)) // Output: 7
Kotlin supports object-oriented programming (OOP) concepts like classes, interfaces, inheritance, and polymorphism.
Classes in Kotlin are defined using the class keyword. By default, they are final unless marked as open.
class Person(val name: String, var age: Int)
Kotlin uses the : symbol for inheritance. A class can inherit from only one parent class.
open class Animal(val name: String) {
open fun makeSound() {
println("Some sound")
}
}
class Dog(name: String) : Animal(name) {
override fun makeSound() {
println("Bark!")
}
}
Interfaces in Kotlin can contain abstract methods and properties, as well as default implementations.
interface Movable {
fun move()
}
class Car : Movable {
override fun move() {
println("Car is moving")
}
}
val Over var: Prefer immutable variables to avoid unintended side effects.NullPointerException.data keyword to automatically generate useful methods like equals(), hashCode(), and toString().fun String.addExclamation(): String {
return this + "!"
}
println("Hello".addExclamation()) // Output: Hello!
Kotlin offers a modern, concise syntax with powerful features for building robust applications. Its interoperability with Java makes it an excellent choice for Android development and beyond. By mastering the basics covered in this tutorial, you are well on your way to becoming proficient in Kotlin.
In the next sections of this course, we will delve deeper into advanced topics such as coroutines, collections, and more. Keep practicing by building small projects and experimenting with different features of Kotlin.