Share
Embed
Kotlin Basics
This presentation gives a quick start for Kotlin for beginners
Rozina Darediya
February 08, 2020
Transcript
46 slides
-
About Me Rozina Darediya Android Developer at Theta Technolabs Kotlin/
Java Enthusiast GDG & WTM Speaker : rozina_02 -
What is Kotlin ❖ Cross-platform ❖ Statically typed programming language
❖ 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 -
How Kotlin? ❖ So, If you are on Android Studio
3.0 you are not required to add Kotlin plugin as they have bundled directly into it. Prior to this version, you need to install Kotlin plugin. -
How Kotlin? ❖ So, If you are on Android Studio
3.0 you are not required to add Kotlin plugin as they have bundled directly into it. Prior to this version, you need to install Kotlin plugin. -
How Kotlin? How to install Kotlin plugin ? Go to
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. -
What difference you see ? ❖ .kt as extension of
file instead .java ❖ added apply plugin: ‘kotlin-android’ ❖ Implementation “org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version” ❖ kotlin_version = '1.3.11' -
Fundamental syntax ❖ Constants and Variables Java String name =
"Kotlin programming"; final String name = "Kotlin programming"; Kotlin var name = "Kotlin programming" val name = "Kotlin programming" -
Fundamental syntax ❖ Assigning the null value Java String otherName;
otherName = null; Kotlin var otherName : String? otherName = null -
Fundamental syntax ❖ Verify if value is null Java if
(text != null) { int length = text.length(); } Kotlin text?.let { val length = text.length } // or simply val length = text?.length -
Fundamental syntax ❖ Concatenation of strings Java String firstName =
"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" -
Fundamental syntax ❖ Check the type and casting ❖ Java
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? -
Fundamental syntax ❖ Check the type and casting (implicit) ❖
❖ 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 } -
Fundamental syntax ❖ Multiple conditions ❖ ❖ ❖ Java if
(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); } -
Fundamental syntax ❖ Multiple Conditions (Switch case) ❖ ❖ ❖
❖ 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" } -
Fundamental syntax ❖ For-loops ❖ ❖ ❖ ❖ ❖ Java
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) { } -
Methods and Class ❖ Defining methods Java void doSomething() {
// logic here } Kotlin fun doSomething() { // logic here } -
Methods and Class ❖ Variable number of arguments Java void
doSomething(int numbers) { // logic here } Kotlin fun doSomething(var numbers: Int) { // logic here } -
Methods and Class ❖ Defining methods with return Java int
getScore() { // logic here return score; } Kotlin fun getScore(): Int { // logic here return score } -
Methods and Class ❖ Defining methods with return Kotlin //
as a single-expression function fun getScore(): Int = score // even simpler (type will be determined automatically) fun getScore() = score // return-type is Int -
Methods and Class ❖ Returning result of an operation Java
int getScore(int value) { // logic here return 2 * value; } Kotlin fun getScore(value: Int): Int { // logic here return 2 * value } -
Methods and Class ❖ Returning result of an operation Kotlin
// 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 -
Methods and Class ❖ Constructors Java public class Utils {
private Utils() { // This utility class is not publicly instantiable } public static int getScore(int value) { return 2 * value; } } -
Methods and Class ❖ Constructors Kotlin class Utils private constructor()
{ companion object { fun getScore(value: Int): Int { return 2 * value } } } Kotlin // another way object Utils { fun getScore(value: Int): Int { return 2 * value } } -
Methods and Class ❖ Getters and Setters Java public class
Developer { private String name; private int age; public Developer(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } -
Methods and Class ❖ Getters and Setters Kotlin data class
Developer(var name: String, var age: Int) -
Benefits of Kotlin vs. Java ❖ Kotlin is more concise
➢ fewer lines ➢ code maintainability ➢ Readability ❖ Kotlin code is safer ➢ prevents common programming mistakes(null safety) ➢ Leads to system failures and application crashes -
Benefits of Kotlin vs. Java ❖ has better support for
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 -
Benefits of Kotlin vs. Java ❖ helps reduce errors and
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 -
Definition ❖ Create an application which has Splash screen, Login
Screen screen Sign up screen and Home screen with logout. ➢ Use Kotlin ➢ Use proper validation ➢ Use proper Project structure -
Links : ❖ Presentation link : https://speakerdeck.com/rozina_02 ❖ Kotlin Sheet
: https://drive.google.com/file/d/1-x12dlx6qSXTk0W7gx_TKUWWolkVq1 V4/view?usp=sharing