Let’s get started

Few things that help you get started easily

Naming

Java:

private String mSomeString = “Hello world”

Kotlin :

private val someString : String = “Hello world”

val name: String //Final (cant change types) var count: Int //Mutable variable

Use camelcase for naming your variables.

val myVal //kotlin myClass.getMyVal() //java

The data type is inferred

val a: Int = 100 // Int is redundant val a = 100 // Int is inferred val b: Int // Do something b = getResultAsInt()

Void is a plain Java class and has no meaning in Kotlin.

Unit → replaces java's void

→ replaces java's Nothing →'a value that never exists'

Methods / functions in kotlin

fun doSomething()

Class naming convention :

Write a property in its own line

class Person(

public open var firstName: String,

public open var lastName: String,

public open var age:Int

) {} //Subclass - do this class Person(

public open var firstName: String,

public open var lastName: String,

public open var age:Int

) : RealmObject() {}

No need of writing getters and setters

class MyClass {

public var myString: String = ""

} //You can call

myClassInstance.myString = "Hello"

Log.d("TAG","Value is ${myClassInstance.myString}) //prints Hello

Neat feature : Print variables using string interpolation

$object or ${object.property}

switch() → when(condition)

return when(object) {

is String -> object + "test"

is Number -> String.valueOf(object)

else -> null

}

Nullability — As a rule of thumb, !! should never be used and ? should be used rarely. Theses checks can often be avoided and doing so will provide for a more stable code-base. Whenever possible, objects and properties should be made to be not nullable.

Extensions functions / Utility functions

It lets us to add functions to existing classes and by doing so reducing a lot of boiler plate code ,without having to touch the source code of such classes.

Extend an entire class

Use added util function

Such functions can be written as you would normally would, and put the name of the class before separated by a point. These functions can be everywhere, usually in an Extensions file which doesn’t even need to contain a class

Example :

class MyClass {



fun doThisAndReturnModified(value:Int) {

return value*2

}

}

Now i need to get the result in a different format but using the above , the method it has cant be modified for new requirement , and let’s say we don’t want to change the entire class by adding a new set of function , we could

MyClass.doThatAndReturnModified(value:Int) {

return value+2

}

Your original class wont be tampered , we just added a new functionality without modifying it

Kotlin Android Extensions

Kotlin Android Extensions is another Kotlin plugin that is included in the regular one, and that will allow to recover views from Activities, Fragments ,Views in an amazing seamless way.

import android.os.Bundle

import android.support.v7.app.AppCompatActivity

import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main) //tvTitle is a TextView in layout activity_main

tvTitle.text = "Hello Kotlin!!" }

} //magic comes from this import

//import kotlinx.android.synthetic.main.activity_main.*

Some nifty examples :

Some learnings on the way :

int → Int

public static final String HOLA ="hola"

converts to

val HOLA: String ="hola" // Kotlin does not know what static is //Better class SomeClass {

companion object{

const val HOLA = "hola"

}

//...

}

Lazy initialisation

val person by lazy {

SomeUtil(parameter1, parameter2)

}

this helps to initialise certain code only once upon the first usage of person Object.

Then , there is

lateinit var myCustomIntentObj: CustomObject // wont crash

... onCreate(...){

myCustomIntentObj = intent.getExtras().get("key") // this init

}

This greatly helps in Dependency injected variables , ex :

@Inject

lateinit var presenter: MyPresenter

When to use lateinit