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

From Java to Kotlin

B6bf611a9e4f70a8455cb175339e0587?s=47 Gerard
March 20, 2018

From Java to Kotlin

Did you hear about Kotlin? Maybe you find the language really cool but you are afraid about the migration? You don't know risks and how you can migrate your professionnal and/or personnal projects? This talk should give you some answers and help you in your reflections!

B6bf611a9e4f70a8455cb175339e0587?s=128

Gerard

March 20, 2018
Tweet

Transcript

  1. FROM JAVA TO Gerard Paligot

  2. WHAT’S KOTLIN?

  3. WHAT’S KOTLIN? - Started in mid 2010 - Avoid huge

    Java codebase - Want a modern, expressive language - Easy to introduce in the existing environment Presentation
  4. WHAT’S KOTLIN? - Full Java interoperability - Compile as fast

    as Java - More concise than Java - Prevent more kinds of errors than Java - Was simpler than Scala Design Goals
  5. WHY USE KOTLIN? - In a future, will replace Java

    as first language - High adoption of the Android community
  6. WHY USE KOTLIN? High adoption of the Android community

  7. WHY USE KOTLIN? - In a future, will replace Java

    as first language - High adoption of the Android community - Easy learning for iOS and Android developers - Null safety - Compilation speed
  8. WHY USE KOTLIN? Compilation speed

  9. WHY USE KOTLIN? - In a future, will replace Java

    as first language - High adoption of the Android community - Easy learning for iOS and Android developers - Null safety - Compilation speed - Less code is better!
  10. HOW TO START? - Experimenting with a legacy Java/Android project

    - Run automatic conversion to Kotlin and learn what changed - Practice more and inform what’s new at each new Kotlin version
  11. HOW TO START // Top level build.gradle file buildscript {

    ext.kotlin_version = '1.2.30' // ... dependencies { // ... classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } // build.gradle file of your app module // ... apply plugin: 'kotlin-android' // ... dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // ... }
  12. HOW TO START // Top level build.gradle file buildscript {

    ext.kotlin_version = '1.2.30' // ... dependencies { // ... classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } // build.gradle file of your app module // ... apply plugin: 'kotlin-android' // ... dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" // ... }
  13. HOW TO START // Top level build.gradle file buildscript {

    ext.kotlin_version = '1.2.30' // ... dependencies { // ... classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } // build.gradle file of your app module // ... apply plugin: 'kotlin-android' // ... dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" // ... }
  14. JAVA TO KOTLIN… JAVA STYLE

  15. JAVA TO KOTLIN… JAVA STYLE public class MyFragment extends Fragment

    { private static final String KEY_TITLE = "KEY_TITLE"; private static final String KEY_MESSAGE = "KEY_MESSAGE"; public static MyFragment newInstance(String newTitle, String newMessage) { Bundle args = new Bundle(); args.putString(KEY_TITLE, newTitle); args.putString(KEY_MESSAGE, newMessage); MyFragment fragment = new MyFragment(); fragment.setArguments(args); return fragment; } }
  16. JAVA TO KOTLIN… JAVA STYLE public class MyFragment extends Fragment

    { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.my_fragment, container); } }
  17. JAVA TO KOTLIN… JAVA STYLE public class MyFragment extends Fragment

    { private TextView mTitleTextView; private TextView mMessageTextView; private Button mSubmitButton; @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mTitleTextView = view.findViewById(R.id.titleTextView); mTitleTextView.setText(getArguments().getString(KEY_TITLE)); mMessageTextView = view.findViewById(R.id.messageTextView); mMessageTextView.setText(getArguments().getString(KEY_MESSAGE)); mSubmitButton = view.findViewById(R.id. submitButton); mSubmitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("MyFragment", "Somebody click on this button!"); Log.d("MyFragment", "Title: " + mTitleTextView.toString() + ", Message: " + mMessageTextView.toString()); } }); } }
  18. JAVA TO KOTLIN… JAVA STYLE public class MyFragment extends Fragment

    { private static final String KEY_TITLE = "KEY_TITLE"; private static final String KEY_MESSAGE = "KEY_MESSAGE"; public static MyFragment newInstance(String newTitle, String newMessage) { Bundle args = new Bundle(); args.putString(KEY_TITLE, newTitle); args.putString(KEY_MESSAGE, newMessage); MyFragment fragment = new MyFragment(); fragment.setArguments(args); return fragment; } }
  19. JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() {

    companion object { private val KEY_TITLE = "KEY_TITLE" private val KEY_MESSAGE = "KEY_MESSAGE" fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }
  20. JAVA TO KOTLIN… JAVA STYLE + private const val KEY_TITLE

    = "KEY_TITLE" + private const val KEY_MESSAGE = "KEY_MESSAGE" class MyFragment : Fragment() { companion object { - private val KEY_TITLE = "KEY_TITLE" - private val KEY_MESSAGE = "KEY_MESSAGE" fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }
  21. JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE =

    "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }
  22. JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE =

    "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" + private val Bundle.title: String class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }
  23. JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE =

    "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" + private val Bundle.title: String + get() = getString(KEY_TITLE) class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }
  24. JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE =

    "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" + private var Bundle.title: String + get() = getString(KEY_TITLE) + set(value) { + putString(KEY_TITLE, value) + } class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }
  25. JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE =

    "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" private var Bundle.title: String get() = getString(KEY_TITLE) set(value) { putString(KEY_TITLE, value) } + private var Bundle.message: String + get() = getString(KEY_MESSAGE) + set(value) { + putString(KEY_MESSAGE, value) + } class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }
  26. JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE =

    "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" private var Bundle.title: String get() = getString(KEY_TITLE) set(value) { putString(KEY_TITLE, value) } private var Bundle.message: String get() = getString(KEY_MESSAGE) set(value) { putString(KEY_MESSAGE, value) } class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() - args.putString(KEY_TITLE, newTitle) - args.putString(KEY_MESSAGE, newMessage) + args.title = newTitle + args.message = newMessage val fragment = MyFragment() fragment.arguments = args return fragment }
  27. JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE =

    "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" private var Bundle.title: String get() = getString(KEY_TITLE) set(value) { putString(KEY_TITLE, value) } private var Bundle.message: String get() = getString(KEY_MESSAGE) set(value) { putString(KEY_MESSAGE, value) } class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.title = newTitle args.message = newMessage val fragment = MyFragment() fragment.arguments = args return fragment } } }
  28. JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() {

    companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.title = newTitle args.message = newMessage val fragment = MyFragment() fragment.arguments = args return fragment } } }
  29. JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() {

    companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { - val args = Bundle() - args.title = newTitle - args.message = newMessage + val args = Bundle().apply { + title = newTitle + message = newMessage + } val fragment = MyFragment() fragment.arguments = args return fragment } } }
  30. JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() {

    companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle().apply { title = newTitle message = newMessage } val fragment = MyFragment() fragment.arguments = args return fragment } } }
  31. JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() {

    companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { - val args = Bundle().apply { - title = newTitle - message = newMessage - } - val fragment = MyFragment() - fragment.arguments = args - return fragment + return MyFragment().apply { + arguments = Bundle().apply { + title = newTitle + message = newMessage + } + } } } }
  32. JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() {

    companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { return MyFragment().apply { arguments = Bundle().apply { title = newTitle message = newMessage } } } } }
  33. JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE =

    "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" private var Bundle.title: String get() = getString(KEY_TITLE) set(value) { putString(KEY_TITLE, value) } private var Bundle.message: String get() = getString(KEY_MESSAGE) set(value) { putString(KEY_MESSAGE, value) } class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { return MyFragment().apply { arguments = Bundle().apply { title = newTitle message = newMessage } } } } }
  34. JAVA TO KOTLIN… JAVA STYLE public class MyFragment extends Fragment

    { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.my_fragment, container); } }
  35. JAVA TO KOTLIN… JAVA STYLE override fun onCreateView(inflater: LayoutInflater?, container:

    ViewGroup?, savedInstanceState: Bundle?): View? { return inflater?.inflate(R.layout.my_fragment, container) ?: LayoutInflater.from(context).inflate(R.layout.my_fragment, container) }
  36. JAVA TO KOTLIN… JAVA STYLE

  37. JAVA TO KOTLIN… JAVA STYLE public class MyFragment extends Fragment

    { private TextView mTitleTextView; private TextView mMessageTextView; private Button mSubmitButton; @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mTitleTextView = view.findViewById(R.id.titleTextView); mTitleTextView.setText(getArguments().getString(KEY_TITLE)); mMessageTextView = view.findViewById(R.id.messageTextView); mMessageTextView.setText(getArguments().getString(KEY_MESSAGE)); mSubmitButton = view.findViewById(R.id. submitButton); mSubmitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("MyFragment", "Somebody click on this button!"); Log.d("MyFragment", "Title: " + mTitleTextView.toString() + ", Message: " + mMessageTextView.toString()); } }); } }
  38. JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() {

    private var mTitleTextView: TextView? = null private var mMessageTextView: TextView? = null private var mSubmitButton: Button? = null override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView!!.text = arguments.getString(KEY_TITLE) mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView!!.text = arguments.getString(KEY_MESSAGE) mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton!!.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView!!.toString() + ", Message: " + mMessageTextView!!.toString()) } } }
  39. JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() {

    private var mTitleTextView: TextView? = null private var mMessageTextView: TextView? = null private var mSubmitButton: Button? = null override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) - mTitleTextView!!.text = arguments.getString(KEY_TITLE) + mTitleTextView!!.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) - mMessageTextView!!.text = arguments.getString(KEY_MESSAGE) + mMessageTextView!!.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton!!.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView!!.toString() + ", Message: " + mMessageTextView!!.toString()) } } }
  40. JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() {

    private var mTitleTextView: TextView? = null private var mMessageTextView: TextView? = null private var mSubmitButton: Button? = null override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView!!.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView!!.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton!!.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView!!.toString() + ", Message: " + mMessageTextView!!.toString()) } } }
  41. JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() {

    - private var mTitleTextView: TextView? = null - private var mMessageTextView: TextView? = null - private var mSubmitButton: Button? = null + private lateinit var mTitleTextView: TextView + private lateinit var mMessageTextView: TextView + private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView!!.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView!!.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton!!.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView!!.toString() + ", Message: " + mMessageTextView!!.toString()) } } }
  42. JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() {

    private lateinit var mTitleTextView: TextView private lateinit var mMessageTextView: TextView private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView!!.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView!!.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton!!.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView!!.toString() + ", Message: " + mMessageTextView!!.toString()) } } }
  43. JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() {

    private lateinit var mTitleTextView: TextView private lateinit var mMessageTextView: TextView private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) - mTitleTextView!!.text = arguments.title + mTitleTextView.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) - mMessageTextView!!.text = arguments.message + mMessageTextView.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) - mSubmitButton!!.setOnClickListener { + mSubmitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView!!.toString() + ", Message: " + mMessageTextView!!.toString()) } } }
  44. class MyFragment : Fragment() { private lateinit var mTitleTextView: TextView

    private lateinit var mMessageTextView: TextView private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView.toString() + ", Message: " + mMessageTextView.toString()) } } } JAVA TO KOTLIN… JAVA STYLE
  45. class MyFragment : Fragment() { private lateinit var mTitleTextView: TextView

    private lateinit var mMessageTextView: TextView private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") - Log.d("MyFragment", "Title: " + mTitleTextView.toString() - + ", Message: " + mMessageTextView.toString()) + Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView") } } } JAVA TO KOTLIN… JAVA STYLE
  46. class MyFragment : Fragment() { private lateinit var mTitleTextView: TextView

    private lateinit var mMessageTextView: TextView private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView") } } } JAVA TO KOTLIN… JAVA STYLE
  47. // Top level build.gradle file buildscript { ext.kotlin_version = '1.2.30'

    // ... dependencies { // ... classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } // build.gradle file of your app module // ... apply plugin: ‘kotlin-android' + apply plugin: 'kotlin-android-extensions' // ... dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" // ... } JAVA TO KOTLIN… JAVA STYLE
  48. class MyFragment : Fragment() { private lateinit var mTitleTextView: TextView

    private lateinit var mMessageTextView: TextView private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView") } } } JAVA TO KOTLIN… JAVA STYLE
  49. + import kotlinx.android.synthetic.main.my_fragment.* class MyFragment : Fragment() { - private

    lateinit var mTitleTextView: TextView - private lateinit var mMessageTextView: TextView - private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView.text = arguments.title - mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView.text = arguments.message - mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView") } } } JAVA TO KOTLIN… JAVA STYLE
  50. import kotlinx.android.synthetic.main.my_fragment.* class MyFragment : Fragment() { override fun onViewCreated(view:

    View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) titleTextView.text = arguments.title messageTextView.text = arguments.message submitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView") } } } JAVA TO KOTLIN… JAVA STYLE
  51. None
  52. KOTLIN TO BINARY TO JAVA

  53. KOTLIN TO BINARY TO JAVA

  54. class MyClass abstract class MyAbsClass open class MyOpenClass data class

    MyDataClass(val field: String) KOTLIN TO BINARY TO JAVA
  55. KOTLIN TO BINARY TO JAVA

  56. public final class MyClass { } public abstract class MyAbsClass

    { } public class MyOpenClass { } KOTLIN TO BINARY TO JAVA
  57. public final class MyDataClass { @NotNull private final String field;

    @NotNull public final String getField() { return this.field; } public MyDataClass(@NotNull String field) { // ... } @NotNull public final MyDataClass copy(@NotNull String field) { // ... } public String toString() { // ... } public int hashCode() { // ... } public boolean equals(Object var1) { // ... } } KOTLIN TO BINARY TO JAVA
  58. open class MethodExamples { fun m() {} open fun mOpen()

    {} } interface Method2Examples { fun mOpen() {} } class Usage : MethodExamples(), Method2Examples{ override fun mOpen() { super<Method2Examples>.mOpen() super<MethodExamples>.mOpen() } } KOTLIN TO BINARY TO JAVA
  59. public class MethodExamples { public final void m() { }

    public void mOpen() { } } public interface Method2Examples { void mOpen(); public static final class DefaultImpls { public static void mOpen(Method2Examples $this) { } } } public final class Usage extends MethodExamples implements Method2Examples { public void mOpen() { Method2Examples.DefaultImpls.mOpen(this); super.mOpen(); } } KOTLIN TO BINARY TO JAVA
  60. class MyClass(val listener: (test: String) -> Unit) { fun m()

    { listener("Result") } } fun main(args: Array<String>) { MyClass { it -> // Do something really cool! } } KOTLIN TO BINARY TO JAVA
  61. public final class MyClass { @NotNull private final Function1 listener;

    public final void m() { this.listener.invoke("Result"); } @NotNull public final Function1 getListener() { return this.listener; } public MyClass(@NotNull Function1 listener) { Intrinsics.checkParameterIsNotNull(listener, "listener"); super(); this.listener = listener; } } KOTLIN TO BINARY TO JAVA
  62. /** A function that takes 0 arguments. */ public interface

    Function0<out R> : Function<R> { /** Invokes the function. */ public operator fun invoke(): R } /** A function that takes 1 argument. */ public interface Function1<in P1, out R> : Function<R> { /** Invokes the function with the specified argument. */ public operator fun invoke(p1: P1): R } /** A function that takes 2 arguments. */ public interface Function2<in P1, in P2, out R> : Function<R> { /** Invokes the function with the specified arguments. */ public operator fun invoke(p1: P1, p2: P2): R } // ... /** A function that takes 22 arguments. */ public interface Function22<in P1, in P2, in P3, in P4, in P5, in P6, in P7, in P8, in P9, in P10, in P11, in P12, in P13, in P14, in P15, in P16, in P17, in P18, in P19, in P20, in P21, in P22, out R> : Function<R> { /** Invokes the function with the specified arguments. */ public operator fun invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9 p10: P10, p11: P11, p12: P12, p13: P13, p14: P14, p15: P15, p16: P16, p17: P17, p18: P18, p19: P19, p P20, p21: P21, p22: P22): R } KOTLIN TO BINARY TO JAVA
  63. None
  64. fun MutableList<Int>.swap(index1: Int, index2: Int) { val tmp = this[index1]

    this[index1] = this[index2] this[index2] = tmp } KOTLIN TO BINARY TO JAVA
  65. public final class ExtensionsKt { public static final void swap(@NotNull

    List $receiver, int index1, int index2) { Intrinsics.checkParameterIsNotNull($receiver, "$receiver"); int tmp = ((Number)$receiver.get(index1)).intValue(); $receiver.set(index1, $receiver.get(index2)); $receiver.set(index2, Integer.valueOf(tmp)); } } KOTLIN TO BINARY TO JAVA
  66. object MyObject { var test: String = "" } fun

    main(args: Array<String>) { MyObject.test = "new value" } KOTLIN TO BINARY TO JAVA
  67. public final class MyObject { @NotNull private static String test;

    public static final MyObject INSTANCE; @NotNull public final String getTest() { return test; } public final void setTest(@NotNull String var1) { Intrinsics.checkParameterIsNotNull(var1, "<set-?>"); test = var1; } static { MyObject var0 = new MyObject(); INSTANCE = var0; test = ""; } } KOTLIN TO BINARY TO JAVA
  68. TRAPS

  69. fun m(typeClass: AbsType) { } open class AbsType class SubType1

    : AbsType() class SubType2 : AbsType() TRAPS
  70. fun m(typeClass: AbsType) { + when (typeClass) { + is

    SubType1 -> println("Typed by subtype 1") + is SubType2 -> println("Typed by subtype 2") + else -> println("No type identified") + } } open class AbsType class SubType1 : AbsType() class SubType2 : AbsType() TRAPS
  71. fun m(typeClass: AbsType) { when (typeClass) { is SubType1 ->

    println("Typed by subtype 1") - is SubType2 -> println("Typed by subtype 2") + is SubType2 -> { + println("Typed by subtype 2") + typeClass.children.forEach { + when (it) { + is SubType1 -> println("Child typed by subtype 1") + is SubType2 -> println("Child typed by subtype 2") + else -> println("No type identified") + } + } + } else -> println("No type identified") } } open class AbsType class SubType1 : AbsType() - class SubType2 : AbsType() + class SubType2(val children: List<AbsType>) : AbsType() TRAPS
  72. fun m(typeClass: AbsType) { when (typeClass) { is SubType1 ->

    println("Typed by subtype 1") is SubType2 -> { println("Typed by subtype 2") typeClass.children.forEach { when (it) { is SubType1 -> println("Child typed by subtype 1") is SubType2 -> println("Child typed by subtype 2") else -> println("No type identified") } } } else -> println("No type identified") } } open class AbsType class SubType1 : AbsType() class SubType2(val children: List<AbsType>) : AbsType() TRAPS
  73. variable.toList() variable.toInt() variable.joinToString { ... } variable.copy() variable.plus(...) variable.minus(...) ...

    variable.doEverything() TRAPS
  74. WTF (˽°□°҂˽Ɨ ˍʓˍ

  75. someObject?.takeIf { status }.apply { doThis() } WTF (˽°□°҂˽Ɨ ˍʓˍ

  76. MyNullSafetyClass var7 = status ? someObject : null; doThis(); WTF

    (˽°□°҂˽Ɨ ˍʓˍ
  77. - someObject?.takeIf { status }.apply { doThis() } + someObject?.takeIf

    { status }?.apply { doThis() } WTF (˽°□°҂˽Ɨ ˍʓˍ
  78. MyNullSafetyClass var7 = status ? someObject : null; if((status ?

    someObject : null) != null) { doThis(); } WTF (˽°□°҂˽Ɨ ˍʓˍ
  79. null.apply { ... } WTF (˽°□°҂˽Ɨ ˍʓˍ

  80. class MyClass class MyClass2(val test: String, val test2: String) class

    MyClass3(val clazz: MyClass2?) fun main(args: Array<String>) { val m: MyClass? = MyClass() // No compilation error. if (m != null) { m.toString() } // No compilation error. "${m!!.toString()}${m.toString()}" // No compilation error. val myClass3 = MyClass3(MyClass2("", "")) val test = myClass3.clazz!!.test val test2 = myClass3.clazz.test2 } WTF (˽°□°҂˽Ɨ ˍʓˍ
  81. class MyClass class MyClass2(val test: String, val test2: String) class

    MyClass3(val clazz: MyClass2?) class MyClass4 { private var myClass2: MyClass2? = null fun m() { val test = myClass2!!.test // error, wtf this shit?! val test2 = myClass2.test2 } } WTF (˽°□°҂˽Ɨ ˍʓˍ
  82. class MyClass class MyClass2(val test: String, val test2: String) class

    MyClass3(val clazz: MyClass2?) class MyClass4 { private var myClass2: MyClass2? = null fun m() { if (myClass2 != null) { val test = myClass2!!.test val test2 = myClass2!!.test2 } } } WTF (˽°□°҂˽Ɨ ˍʓˍ
  83. SUMMARY - Kotlin is mature and ready for production -

    Designed to be compatible and better than Java - Easy to learn and high adoption by the Android community - Be curious
  84. FROM JAVA TO Gerard Paligot