1/42
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Modern, trending programming language that was released in 2016 by JetBrains
Kotlin
Kotlin is a modern, trending programming language that was released in __ by JetBrains
2016
Kotlin is a modern, trending programming language that was released in 2016 by __
JetBrains
Compatible with Java
Kotlin
Works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
Kotlin
Concise and safe
Kotlin
Easy to learn, especially if you already know Java
Kotlin
Free to use
Kotlin
Big community/support
Kotlin
Mobile applications (specially Android apps)
Web development
Server side applications
Data science
And much, much more!
Uses of Kotlin

The __ keyword is used to declare a function
fun

The __ function is needed to execute a program
main()

In Kotlin, code statements do not have to end with a __ (_)
semicolon (;)

//Single line comments

/* … */Multi-line comments

The __ keyword can create a variable whose reference will be immutable (which cannot be modified after its initialization)
val

The __ keyword lets you create a variable whose reference will be mutable (which can be modified after its initialization)
var

Kotlin does not require you to mention the __ __ __ __ when declaring it (thanks to type inference)
type of a variable

val time = 20
val greeting = if (time < 18) {
"Good day."
} else {
"Good evening."
}
println(greeting)if
if – else
if – else if
if – else expression

val time = 20
val greeting = if (time < 18)
"Good day." else "Good evening."if
if – else
if – else if
if – else expression

val day = 4
val result = when (day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4 -> "Thursday"
5 -> "Friday"
6 -> "Saturday"
7 -> "Sunday"
else -> "Invalid day."
}
println(result)
// Outputs "Thursday" (day 4)when expression

val nums = arrayOf(1, 5, 10, 15, 20)
for (x in nums) {
println(x)
}// for loop using arrays
while
do – while
for – loop

for (nums in 5..15) {
println(nums)
}// for loop using range
while
do – while
for – loop

class Car {
var brand = ""
var model = ""
var year = 0
}class declaration (OOP)

// Create a c1 object of the Car class
val c1 = Car()
// Access the properties and add some values to it
c1.brand = "Ford"
c1.model = "Mustang"
c1.year = 1969
println(c1.brand) // Outputs Ford
println(c1.model) // Outputs Mustang
println(c1.year) // Outputs 1969class instantiation (OOP)

class Car(var brand: String, var model: String, var year: Int)
fun main() {
val c1 = Car("Ford", "Mustang", 1969)
}using constructor (OOP)

class Car(var brand: String, var model: String, var year: Int) {
// Class function
fun drive() {
println("Wrooom!")
}
// Class function with parameters
fun speed(maxSpeed: Int) {
println("Max speed is: " + maxSpeed)
}
}
fun main() {
val c1 = Car("Ford", "Mustang", 1969)
// Call the functions
c1.drive()
c1.speed(200)
}class & functions (OOP)

class Person (val _name: String) {
// Member Variables
var name: String
// Initializer Blocks
init {
println("This is first init block")
}
init {
println("This is second init block")
}
init {
println("This is third init block")
}
init {
this.name = _name
println("Name = $name")
}
}
fun main(args: Array<String>) {
val person = Person("Abe")
}Using init blocks (OOP)


Default value in primary constructor (OOP)

Using the secondary constructor (OOP)

inheritance (OOP)
Kotlin vs. Java:
Features: Extension Functions
Kotlin: It is already available in Kotlin
Java: In java, we need to create class
Kotlin vs. Java:
Features: Null Safety
Kotlin: It is available in Kotlin
Java: It is not available in Java
Kotlin vs. Java:
Features: Static Members
Kotlin: Kotlin doesn’t have a static member for a class
Java: It is available in Java
Kotlin vs. Java:
Features: String Templates
Kotlin: Yes, there are two types of string literals in Kotlin
Java: It is available in Java too but it doesn’t support expression like Kotlin
Kotlin vs. Java:
Features: Wildcard Types
Kotlin: It is not available in Kotlin
Java: Available in Java
Kotlin vs. Java:
Features: Smartcasts
Kotlin: Available in Kotlin
Java: Not Available in Java
Kotlin vs. Java:
Features: No Checked Exception
Kotlin: Kotlin removed exceptions entirely
Java: It is problematic in Java
Kotlin vs. Java:
Features: Operator Overloading
Kotlin: Kotlin allows users to provide a way to invoke functions
Java: Operators are tied to particular Java Types
Kotlin vs. Java:
Features: Constructors
Kotlin: It has primary constructor and secondary constructor
Java: Constructors can be used to take parameters to initialize attributes
Kotlin vs. Java:
Features: Type System
Kotlin: It gives nullability support, type inference, and universal guards
Java: There are other kinds of reference types related to the basic concept of class

Kotlin in Android

Kotlin in Android (Java)