❖ General-purpose programming language ❖ Type inference ❖ Developed by JetBrains ❖ First introduced in 2011 and a new language for the JVM ❖ It is object-oriented language ❖ Kotlin is sponsored by Google, announced as one of the official languages for Android Development in 2017
File|Settings|Plugins|Install JetBrains plugin… then search “Kotlin” and install it. If you are looking at “Welcome to Android Studio” screen, choose Configure|Plugins|Install JetBrains plugin… You’ll need to restart the Android Studio to see changes after installation completes.
"Ayusch"; String lastName = "Jain"; String message = "My name is: " + firstName + " " + lastName; Kotlin val firstName = "Ayusch" val lastName = "Jain" val message = "My name is: $firstName $lastName"
if (object instanceof Car) { } Car car = (Car) object; Kotlin if (object is Car) { } var car = object as Car // if object is null var car = object as? Car //var car = object as Car?
❖ Java if (object instanceof Car) { Car car = (Car) object; } Kotlin if (object is Car) { var car = object // smart casting } // if object is null if (object is Car?) { var car = object // smart casting, car will be null }
(score >= 0 && score <= 300) { } ❖ for each Kotlin if (score in 0..300) { } f Java for (Car car : cars) { System.out.println(car.speed); } Kotlin for (Car car : cars) { System.out.println(car.speed); }
❖ Java int score = // some score; String grade; switch (score) { case 10: case 9: grade = "Excellent"; break; case 8: case 7: case 6: grade = "Good"; break; case 5: case 4: grade = "OK"; break; case 3: case 2: case 1: grade = "Fail"; break; default: grade = "Fail"; } Kotlin var score = // some score var grade = when (score) { 9, 10 -> "Excellent" in 6..8 -> "Good" 4, 5 -> "OK" in 1..3 -> "Fail" else -> "Fail" }
for (int i = 1; i <= 10 ; i++) { } for (int i = 1; i < 10 ; i++) { } for (int i = 10; i >= 0 ; i--) { } for (int i = 1; i <= 10 ; i+=2) { } for (int i = 10; i >= 0 ; i-=2) { } for (String item : collection) { } Kotlin for (i in 1..10) { } for (i in 1 until 10) { } for (i in 10 downTo 0) { } for (i in 1..10 step 2) { } for (i in 10 downTo 0 step 2) { } for (item in collection) { }
as a single-expression function fun getScore(): Int = score // even simpler (type will be determined automatically) fun getScore() = score // return-type is Int
// as a single-expression function fun getScore(value: Int): Int = 2 * value // even simpler (type will be determined automatically) fun getScore(value: Int) = 2 * value // return-type is int
{ companion object { fun getScore(value: Int): Int { return 2 * value } } } Kotlin // another way object Utils { fun getScore(value: Int): Int { return 2 * value } }
functional programming ➢ allows developers to solve many tasks more easily and consistently ➢ It was also introduced in Java 8 ➢ but Kotlin has better support for functional programming ❖ speeds up every-day development tasks ➢ These include default parameter values, ➢ object declarations, ➢ extension functions, ➢ and many more
bugs in the code ➢ The Kotlin compiler aims to fail-fast whenever possible ➢ performs many checks, ➢ Avoids runtime errors and ➢ Reduce the cost and effort of error fixes. ❖ The code base shrinks and increases in quality ➢ Maintainability, ➢ Readability ➢ Fewer lines of code that are easier to maintain