Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android City-The walls of Kotlin Street

Android City-The walls of Kotlin Street

Time constraint and productivity are two major things kept in mind when working on any Android project, which could not be easily achieved by the boilerplate code and innumerable exception handlers in Java. Thus, it was time to introduce a new and more mature language, Kotlin

Temidayo Adefioye

June 19, 2018
Tweet

Other Decks in Programming

Transcript

  1. Signs on the Street of Kotlin! 2 What are these

    “!!”? Why is my code littered with “?” and “:”?
  2. HELLO! I am Temidayo Adefioye I am here because I

    love to talk about technology. You can find me at @temidjoy 3 https://medium.com/@temidjoy Geek Of Thrones
  3. “Kotlin is a statically typed programming language that runs on

    the Java virtual machine(JVM) and also can be compiled to Js source code. Its primary development is from a team of JetBrains Developers based in Russia 5
  4. Kotlin vs Java ▪ Java is obsolete...very obsolete ▪ Verbosity

    and ceremony ▪ Java is vulnerable to error ▪ Goodbye to NullPointerExceptions ▪ Write less code, be happy ▪ Kotlin interoperability with Java is 100% Kotlin can help make your developer life a lot more easier! 6
  5. Extension Methods 8 A handy way of extending existing classes

    with a new functionality without using inheritance or any forms of the Decorator patterns Java Code String escaped = escapeStringForXml(input ); Kotlin Code val escaped = input.escapeForXml()
  6. Extension Methods 9 Context Adjusting Extension Methods val name =

    maybeGetName() val uppercase = name?.let { n -> n.toUpperCase() } Custom Extension Method fun String.escapeForXml() : String { return this .replace("&", "&amp;") .replace("<", "&lt;") .replace(">", "&gt;") } Generic Extension Methods fun <T> T.concatAsString(b: T) : String { return this.toString() + b.toString() }
  7. Lambdas Expression Syntax 11 //Create function fun saySomething(name: String, f:

    (String) -> Unit) { f(name) } //Call function saySomething("Kotlin", {name -> println("Hello $name")}) - A lambda expression is always surrounded by curly braces. - Parameter declarations in the full syntactic form go inside curly braces and have optional type annotations - The body goes after an -> sign. - If the inferred return type of the lambda is not Unit, the last (or possibly single) expression inside the lambda body is treated as the return value.
  8. Null Safety 13 var artist: Artist? = null var notNullArtist:

    Artist = null artist?.print() artist.print() if (artist != null) { artist.print() }// Use Elvis operator to give an alternative in case the object is null val name = artist?.name ?: “empty” // Only use it when we are sure it ́s not null. // Will throw an exception otherwise. artist!!.print()
  9. class Hello : Greeting { private val greeting: String =

    “hello” override fun greet() { print(greeting) } } 15 Data Classes
  10. Why do I care much about Kotlin? It’s safer I

    can write more productive code! Concise and Readable 16 I can Leverage Java’s rich ecosystem and JVM platform
  11. val emailStr = email.text.toString() val passwordStr = password.text.toString() 19 Kotlin

    EditText email=(EditText)findViewById( R.id.email); EditText password=(EditText)findView ById(R.id.password); Java
  12. private fun isEmailValid(email: String): Boolean { return email.contains("@") } 20

    Kotlin private boolean isEmailValid(String email){ return email.contains("@") } Java
  13. Project Source To see the entire source code for this

    kotlin project ▪ https://github.com/Temidtech/SimpleLoginKotlin 21