You are on page 1of 18

Kickstart Kotlin

Zahidur Rahman Faisal


Why use Kotlin?

● Simple syntax, easy to read / write


● Powerful features from many other modern languages
● Goodbye NullPointerExceptions
● Completely interoperable with Java
● Great IDE and tooling support
● Code less, do more
● Similarity with Swift, JavaScript or Python
● Because learning is fun and JAVA is too old-school now! :)
Kickstart Kotlin

Preferences (OSX)

or Settings (Windows/Linux)

> Plugins

> Browse Repositories

> Search “Kotlin”

> Restart Android Studio


Convert your JAVA class

> Select your .java file (Ex: MainActivity.java)

> Code

> Convert Java file to Kotlin file


Goodbye NPE
var nonNullString: String = "Droidcon" var nullableString: String? = "Droidcon"
nonNullString = null // Compilation error nullableString = null // Compiles fine

val size = nullableString.length // Error:


variable 'nullableString' can be null
val size = nonNullString.length
val size = nullableString?.length // Null safe
Elvis (Presley?) to the rescue

// Returns length of the String -1 if the String is null


val size = nullableString?.length ?: -1
No insane sanity checks

class Droidcon { class Country { class Venue {


var country: Country? var venue: Venue? var location: String?
var host: String? var ticketPrice: Int var vanueName: String?
} } }

JAVA Kotlin

if (droidCon != null) { print(droidCon?.country?.venue?.location)


if (droidCon.country != null) {
if (droidCon.country.venue != null) {
... ... ...
println(droidCon.country.venue.location);
}
}
}
POJO vs Data Class

POJO Data Class

public class DroidconEvent { data class DroidconEvent(val topic: String,


var speaker: String, val time: Long)
public String topic;
public String speaker;
public long time;

public DroidconEvent(String topic, String speaker, long time) {


this.topic = topic;
this.speaker = speaker;
this.time = time;
}

}
Lazy Lambdas

JAVA Kotlin
mButton.setOnClickListener(new View.OnClickListener() { mButton.setOnClickListener {
Toast.makeText(this, "Hello Droidcon!", Toast.LENGTH_SHORT).show()
@Override
}
public void onClick(View v) {
Toast.makeText(this, "Hello Droidcon!",
Toast.LENGTH_SHORT).show();
}
};
Set with “with()”

JAVA Kotlin

mTextView.setText("Hello Droidcon"); with (mTextView) {


mTextView.setTextSize(30f); text = "Hello Droidcon!"
mTextView.setTextColor(Color.WHITE); textSize = 30f
mTextView.setBackgroundColor(Color.BLACK); setTextColor(Color.WHITE)
setBackgroundColor(Color.BLACK)
}
“When” is the new “Case”

when (x) {
0 -> print("x is 0") // x == 0
in 1..10 -> print("x is in the range") // x inside range 1 - 10
in validNumbers -> print("x is valid") // x is in a collection “validNumbers”
!in 10..20 -> print("x is outside the range") // x is outside range 10 - 20
else -> print("none of the above") // default case
}
Exclusive Extensions

Extension function: (for navigating to a new Activity)


fun Activity.navigateTo(activity: Activity, bundle: Bundle?) {
val intent = Intent(this, activity.javaClass)
bundle ?: intent.putExtra("bundle", bundle)
startActivity(intent)
}

Usage: (from any Activity)


navigateTo(AnotherActivity(), null)
Funny Functions: Default arguments
fun incrementSteps(currentSteps: Int, increment: Int = 1): Int {
return currentSteps + increment
}

Usage:

incrementSteps(5) // Returns 6
incrementSteps(5, 3) // Returns 8
Funny Functions: Named Arguments
fun incrementSteps(currentSteps: Int, increment: Int = 1): Int {
return currentSteps + increment
}

Usage:

incrementSteps(currentSteps = 5, increment = 2) // Returns 7


Funny Functions: Single-Expression Functions
fun doubleSteps(currentSteps: Int) = currentSteps * 2

Usage:

doubleSteps(5) // Returns 10
Funny Functions: Varargs (Variable Arguments)
fun getSum(vararg numbers: Int): Int {
var sum = 0
for (number in numbers) {
sum += number
}
return sum
}

Usage:

getSum(1, 2, 3) // Returns 6
getSum(1, 2, 3, 4, 5) // Returns 15
More Kotlin Awesomeness

https://kotlin.link

http://try.kotlinlang.org

https://github.com/Kotlin/kotlin-koans

https://www.facebook.com/groups/1289612087746759/
Thank You

You might also like