Fundamentals of Kotlin Programming Language
Kotlin is a modern, statically typed programming language developed by JetBrains.
2025-02-17T07:35:26.711Z Back to posts
Fundamentals of Kotlin Programming Language
=====================================================
Table of Contents
Introduction to Kotlin
Kotlin is a modern, statically typed programming language developed by JetBrains. It’s designed to be concise, safe, and interoperable with Java code. Kotlin compiles to JVM (Java Virtual Machine) bytecode, making it a great choice for Android app development.
Kotlin Basics
Variables and Data Types
In Kotlin, you can declare variables using the val
or var
keyword. The val
keyword is used for immutable variables, while var
is used for mutable variables.
// Immutable variable declaration
val name: String = "John Doe"
// Mutable variable declaration
var age: Int = 25
Kotlin has several built-in data types:
Data Type | Description |
---|---|
Int | Whole numbers, e.g., 1, 2, 3, etc. |
String | Sequence of characters, e.g., “Hello”, “World”, etc. |
Boolean | True or false values |
Double | Decimal numbers, e.g., 3.14, -0.5, etc. |
Control Flow Statements
Kotlin has several control flow statements:
- Conditional statements:
if
,when
- Loops:
for
,while
// Conditional statement
val x = 10
if (x > 5) {
println("x is greater than 5")
}
// When expression
val color = "Red"
when (color) {
"Red" -> println("The color is red")
"Green" -> println("The color is green")
else -> println("The color is neither red nor green")
}
Functions in Kotlin
Functions in Kotlin are defined using the fun
keyword. You can also use function literals to define functions inline.
// Function definition
fun greet(name: String) {
println("Hello, $name!")
}
// Function literal
val greet = fun(name: String) {
println("Hello, $name!")
}
Object-Oriented Programming in Kotlin
Classes and Objects
Classes in Kotlin are defined using the class
keyword. You can define constructors, properties, and methods within a class.
// Class definition
class Person(val name: String, val age: Int) {
fun greet() {
println("Hello, my name is $name and I'm $age years old.")
}
}
// Creating an object of the class
val person = Person("John Doe", 25)
person.greet()
Inheritance in Kotlin
Kotlin supports single inheritance. You can use the : Class
syntax to inherit from a parent class.
// Parent class definition
open class Animal {
open fun sound() {
println("The animal makes a sound.")
}
}
// Child class definition
class Dog : Animal() {
override fun sound() {
println("The dog barks.")
}
}
Polymorphism in Kotlin
Kotlin supports method overriding and method overloading. You can use the override
keyword to override methods, and use function parameters to overload methods.
// Method overriding
open class Shape {
open fun area() {
println("The shape has an area.")
}
}
class Circle : Shape() {
override fun area() {
println("The circle has a radius of 5 units and an area of 78.54 square units.")
}
}
Kotlin Collections
Lists in Kotlin
Kotlin lists are defined using the List
interface.
// List definition
val numbers = listOf(1, 2, 3, 4, 5)
println(numbers.size) // Output: 5
// Accessing elements
val firstNumber = numbers[0]
println(firstNumber) // Output: 1
// Modifying the list
numbers.add(6)
println(numbers.size) // Output: 6
Sets and Maps in Kotlin
Kotlin sets are defined using the Set
interface, while maps are defined using the Map
interface.
// Set definition
val numbers = setOf(1, 2, 3, 4, 5)
println(numbers.size) // Output: 5
// Map definition
val map = mapOf("John" to 25, "Alice" to 30)
println(map["John"]) // Output: 25