Updated 6 November 2017
In this blog, we’re going to take a look at Kotlin, a new language in the JVM world, and some of its basic features, including classes, inheritance, conditional statements, and looping constructs.
1 2 3 4 5 6 7 |
fun sum(a: Int, b: Int): Int { return a + b } //Function with an expression body and inferred return type fun sum(a: Int, b: Int)=a+b |
1 2 3 4 |
val a: Int = 1 // immediate assignment val b = 2 // `Int` type is inferred val c: Int // Type required when no initializer is provided c = 3 // deferred assignment |
1 2 3 4 5 6 7 8 |
when(a){ 1->fun sum(a: Int, b: Int)= a+b 2->fun sub(a: Int, b: Int)=a-b else-> a } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
val items = listOf("apple", "banana", "kiwi") for (item in items) { println(item) } // While loop val items = listOf("apple", "banana", "kiwi") var index = 0 while (index < items.size) { println("item at $index is ${items[index]}") index++ } |
A class in Kotlin can have a primary constructor and one or more secondary constructors. The primary constructor is part of the class header it goes after the class name (and optional type parameters).
1 2 3 4 5 6 7 8 9 |
class Person constructor(firstName: String) { } // Secondary Constructor class Person { constructor(parent: Person) { parent.children.add(this) } } |
Have you notice there no semicolon in sentences in Kotlin you don’t have to put the semicolon at the end of sentences. Null-safety is an important feature of the Kotlin language. It helps developers avoid NullPointerExceptions and improves the quality of their apps.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.