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

Using Kotlin in Android

Ivan
May 03, 2019

Using Kotlin in Android

Kotlin examples for study group in Taiwan Kotlin User Group

Ivan

May 03, 2019
Tweet

More Decks by Ivan

Other Decks in Programming

Transcript

  1. Null Safety • Eliminate the danger of null references from

    code • Nullable types and Non-Null Types var a: String = "abc" a = null // compilation error var b: String? = "abc" b = null // ok 3 Nullable types Non-Null types
  2. Example 1 4 Bundle args = getArguments(); if (args !=

    null) { Bundle data = args.getBundle(KEY_DATA); if (data != null) { Log.d(TAG, data.getString(KEY_NAME)); } } Java
  3. Example 1 5 Bundle args = getArguments(); if (args !=

    null) { Bundle data = args.getBundle(KEY_DATA); if (data != null) { Log.d(TAG, data.getString(KEY_NAME)); } } Log.d(TAG, arguments?.getBundle(KEY_DATA)?.getString(KEY_NAME)) Java Kotlin
  4. Data Class • It automatically generates ◦ equals() / hashCode()

    ◦ getters and setters ◦ toString() ◦ copy()
  5. Example 3 9 private void sendInfo(String content, long Time, String

    tag) { /* ... */ } private void sendInfo(String content, long Time) { /* ... */ } private void sendInfo(String content, String tag) { /* ... */ } private void sendInfo(String content) { /* ... */ } Java
  6. Example 3 10 private fun sendInfo( content: String, time: Long

    = Date().time, tag: String = TAG ) { … } sendInfo("content", time = Date().time) Kotlin