kotlin-playground

Kotlin is an statically typed programming language. Kotlin combines OO and functional features and is focused on interoperability, safety, clarity, and tooling support. It currently targets the following

  • JVM
  • Android
  • JavaScript
  • Native.

My favourite Features

concise syntax → code that is easier to read and more pleasant to write; null-safety → reduces number of unexpected "NullPointerException"s, reduces cognitive load for design-by-contract approach (what exact data type is expected); data classes, sealed classes → helps to describe domain models easier and faster; named parameters, default parameters, extension functions (and other syntax sugar) → more readable and concise code, ability to write DSL-like or declarative APIs; coroutines → comes very handy when you are designing multithreaded system (writing non-blocking code); nice tooling support in Intellij IDEA; ability to compile code for JVM, JS and native platforms (in beta now); and (that's very important as well) great community.


Why or Advantages

  • Kotlin is 100% interoperable with the Java programming. Kotlin is completely compatible with Java, developers can incrementally convert their apps.
  • More concise. Very less number of lines of code and easier to manage.
  • More type safe
    • Support to non-nullable types (Null-safety)
    • Type inference
  • Combination of OOP and functional programmings
    • Higher order functions
    • Type Safe Code ((No Raw Types)) / Smart casting
    • No Checked Exceptions
    • Data classes
    • Default / Named parameters
    • Extension functions
    • Coroutines
  • Kotlin developed to fix Java's pain points
  • NPE (Inbuilt Null Safety): It assumes variables can’t be “null” unless a developer explicitly make it that way. This makes Kotlin apps more stable and less likely to crash.
  • https://hackr.io/blog/kotlin-vs-java

Variables

Local type inference: compiler will infer the types

val
  • Read only
  • Similar to final in java
Compile error:
 val cannot be reassigned
var

Mutable

Functions

  • function with a block body and expression body
  • Interoperability with java
  • Unit similar to void. expression body is omitted.
  • Function Types
    • Top level - Similar to static method
    • Member function
    • Local Function
  • Can provide default values for argument in function
  • Named arguments - Can use parameter name in function call
    • By default, there is the direct correspondence between unnamed arguments and parameters according to their order.
    • In Java overloading used: define different overloads and use nested method call
    • Have to specify the for all values when call from Java
    • @JvmOverloads : Instructs kotlin compiler to generate overloads
fun functionMax(a: Int, b:Int) : Int {
 return if(a>b) a else b
 }

or

fun functionMax(a: Int, b:Int) : Int = if(a>b) a else b

Conditionals

  • No ternary operator in Kotlin
  • switch => when
    • No need break statement
    • When used for more than two branches

Loops

  • for-loop
  • while / do-while loop

Ranges

  • in

    iteration

    check for belonging

Exceptions

Reference

Video Links

Articles