Kotlin_TutorialPoints Flashcards
Language type?
Strongly typed
OOP
Functional
When is Kotlin made official language for Adroid Dev?
2017
Prerequisites
Knowledge of JAVA, OOP and Class(es)
Kotlin VS Java
Kotlin: Open Source Software More expressive Less boiler plate Better preformance
Kotlin Functions
Allowed to be declared at the top level
Declarations?
Doesn’t need to be Static
Be careful - can cause problems for Devs coming from Java
Hello World .kt
fun main(args: Array){ println("Hello, World") }
How does run Code on the JVM (Java Virtual Machine)?
byte code!
Similar to Java’s (.class)
Byte code behavior when in the same JVM?
They can communicate with eachother
How does Kotlin translate into JavaScript?
.kt file is converted into ES5.1
Declaring a ‘character’?
Assigning it?
Printing it?
val letter: Char
letter = ‘A’
println(“$letter”)
Declaring Boolean?
val letter: Boolean
letter = true
println(“Your character value is “+”$letter”)
Declaring Strings?
var rawString :String
val escapedString : String
Declaring Arrays?
val numbers: IntArray = intArray(1,2,3,4,5)
Types of Collections
Immutable Collection - cannot be edited
Mutable Collection - can be edited
Collection Caution
Kotlin system does not represent a difference between the 2 Types
Collection - Mutable
val numbers: MutableLIst = mutableListOf(1,2,3)
Collection - Immutable
val readOnlyView: List = numbers
Adding item to Mutable Collection
.add(item)
Collection Functions
first( ) - gets first elem
last( ) - gets last
filter( ) - filters through collection
How does Ranges work in Kotlin?
rangeTo( )
or (. .)
if(i in 1..10) //same as 1<=i && i < = 10
if - else
if ( ) { //code } else { //otherwise code }
Use of When
val x:Int = 5
when (x) {
1 -> print(“x = = 1”)
2 -> print(“x = = 2”)
else -> { // Note the block print("x is neither 1 nor 2") } }
For Loop
val items = listOf((1,2,3,4)
for (i in items) println(‘values of the array” +i)
For loop - looping, index and item at that index
val items = listOf(1, 22, 83, 4)
for ((index, value) in items.withIndex()) {
println(“the element at $index is $value”)
}
While - standard loop
while(x <=10){
println( x )
x++
}
Return
Return is a keyword that returns some value to the calling function from the called function.
Continue & Break
Continue & Break − Continue and break are the most vital part of a logical problem. The “break” keyword terminates the controller flow if some condition has failed and “continue” does the opposite
Example Function declaration
fun doubleMe(x:Int):Int { return 2*x; }
What does a break with a Label on it look like?
myLabel@
break@myLabel
OOP - 1st line - declaration
class myClass{
}
Declaring a private variable within a Class
private var name: String = “Tutorials.point”
val VS var
val - cannot be changed
var - can be changed
What is a Nested Class?
A class that has been created inside another class.