EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Kotlin

Kotlin Overview

Kotlin is a statically typed, general-purpose programming language created by JetBrains, renowned for developing top-notch IDEs like IntelliJ IDEA, PhpStorm, and AppCode. First introduced in 2011, Kotlin is a new language for the JVM (Java Virtual Machine) that offers an object-oriented programming paradigm. It is often regarded as a “better language” than Java while maintaining full interoperability with existing Java code.

In 2017, Google endorsed Kotlin as one of the official languages for Android development.

Example of Kotlin:

fun main() {
    println("Hello World")
}

Key Features of Kotlin:

1. Statically Typed: In Kotlin, the type of every variable and expression is determined at compile time. Although it is a statically typed language, it doesn’t require you to explicitly define the type for every variable.
2. Data Classes: Kotlin includes Data Classes that automatically generate boilerplate code such as equals, hashCode, toString, and getters/setters. For example:

Java Code:

class Book {
    private String title;
    private Author author;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }
}

Output:

data class Book(var title: String, var author: Author)
  • Conciseness: Kotlin significantly reduces the amount of code required compared to other object-oriented programming languages.
  • Safety: Kotlin helps prevent the notorious NullPointerExceptions by incorporating nullability into its type system. By default, every variable in Kotlin is non-null.
val s: String = "Hello World" // Non-null
// The following line will produce a compile-time error:
// s = null

To assign a null value, a variable must be declared as nullable:

var nullableStr: String? = null // Compiles successfully
  • The length() function is also disabled for nullable strings.
  • Interoperability with Java: Since Kotlin runs on the JVM, it is fully interoperable with Java, allowing seamless access to Java code from Kotlin and vice versa.
  • Functional and Object-Oriented Capabilities: Kotlin offers a rich set of features, including higher-order functions, lambda expressions, operator overloading, and lazy evaluation. A higher-order function is one that takes a function as a parameter or returns a function.Example of a Higher-Order Function:
fun myFun(company: String, product: String, fn: (String, String) -> String): Unit {
    val result = fn(company, product)
    println(result)
}

fun main() {
    val fn: (String, String) -> String = { org, portal -> "$org develops $portal" }
    myFun("JetBrains", "Kotlin", fn)
}

Smart Casts: Kotlin automatically typecasts immutable values and ensures safe casting.

fun main() {
    var string: String? = "BYE"
    // This line will produce a compile-time error:
    // print(string.length)
}

With smart casting:

fun main() {
    var string: String? = "BYE"
    if (string != null) { // Smart cast
        print(string.length)
    }
}

1. Compilation Time: Kotlin has a high performance and fast compilation time.
2. Tool-Friendly: Kotlin has excellent support in various IDEs. You can run Kotlin programs using any Java IDE such as IntelliJ IDEA, Eclipse, and Android Studio, as well as from the command line.

End of lesson.