Kotlin Programming Language Tutorial Notes

Jul 25, 2024

Kotlin Programming Language Tutorial Notes

Introduction to Kotlin

  • Kotlin: A statically typed programming language developed by JetBrains
  • Popular since its inception in 2011
  • Primary language for Android development
  • Tutorial Goals:
    • Transition from basic topics to advanced concepts like modeling data and higher order functions
    • Understand application building across mobile, web, and native code

Setting Up Kotlin Environment

  1. Install JetBrains IDE (IntelliJ IDEA)

    • Available for Windows, macOS, and Linux
    • Choose between Ultimate Edition (paid, full features) and Community Edition (free, JV, and Android support)
    • Download and install the IDE
  2. Create Your First Kotlin Project

    • Open IntelliJ IDEA and create a new project
    • Select Kotlin, and then choose Kotlin Module for JVM Target
    • Name the project, e.g., hello_kotlin
    • Add a Kotlin file, main.kt, for your code
  3. Hello World Example

    • Define the main function:
      fun main() {
          println("Hello Kotlin")
      }
      
    • Run your code and observe output.
    • Optional: Use IntelliJ live templates to auto-generate the main function structure.

Kotlin Basics: Variables

  • Variable Types in Kotlin:
    • Mutable Variables: var (reassignable)
    • Immutable Variables: val (assigned once)
  • Declaring Variables:
    val name: String = "Nate"
    var age: Int = 25
    
  • Type System:
    • Non-nullable by default, use ? for nullable types.

Control Flow

  • If Statements:
    if (condition) {
        // do something
    } else {
        // do something else
    }
    
  • When Statements (similar to switch):
    when (variable) {
        condition -> action
        else -> default
    }
    

Functions in Kotlin

  • Define functions with fun keyword:
    fun greet(name: String): String {
        return "Hello, $name" 
    }
    
  • Default and Named Parameters:
    • Provide default values in function definitions, e.g., fun greet(name: String = "Guest").
    • Use named arguments to specify function parameters in any order.

Classes and Objects

  • Defining a Class:
    class Person(val name: String, var age: Int) {
        fun printInfo() {
            println("Name: "+name+", Age: "+age)
        }
    }
    
  • Creating Objects:
    val person = Person("Nate", 25)
    person.printInfo()
    
  • Inheritance:
    • Use open to allow classes to be inherited.
  • Data Classes:
    • Used for data models, auto-generates necessary methods like equals, hashCode, and toString.

Collections and Iteration

  • Working with Collections:
    • Lists, Arrays, and Maps
    • Use functional operations like map, filter
    val numbers = listOf(1, 2, 3, 4, 5)
    val even = numbers.filter { it % 2 == 0 }
    
  • Higher Order Functions:
    • Functions that take other functions as parameters or return functions.
    fun <T> List<T>.customFilter(predicate: (T) -> Boolean): List<T> {
        val result = mutableListOf<T>()
        for (item in this) {
            if (predicate(item)) {
                result.add(item)
            }
        }
        return result
    }
    

Summary

  • Key Features of Kotlin:
    • Null Safety
    • Extension Functions
    • Functional Programming Capabilities
    • Supports OOP principles such as Classes, Objects, Inheritance, and Data Classes

Conclusion

  • With these concepts, you should feel comfortable writing and understanding Kotlin code for various applications.