Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Have you done it yet (Android)?  RSS feed

 
Saloon Keeper
Posts: 1692
68
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kotlin is fully supported by Android Studio (via plugin). You can convert an Java file to an Kotlin file inside of Android Studio with a few simple keystrokes.
Have you done this yet? And if so, how much of your Android project did you convert from Java to Kotlin.

For 'fun' I took an POJO that I had in Android and made it a POKO. The POJO was about 100 lines, the POKO was about 30 lines.
It's nice not having to put in the extra code for the getters and setters.  Usually when you start out with an POJO you setup code for the getters and setters that's fairly generic. With POKO you don't need that.
I'm sure that there are other things in Kotlin as well that are well worth investigating.

Anyways, I don't think I should stray too far from the tutorial I'm following which only use Java.

If POJO means Plain Old Java Object then POKO means Plain Old Kotlin Object.
 
author & internet detective
Marshal
Posts: 38367
645
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. Can you share what the POKO looks like?
 
Pete Letkeman
Saloon Keeper
Posts: 1692
68
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, I'll provide both for comparing and both compile to the same.
I'm currently working through Big Nerd Ranch: Android Programming 3rd edition now that I have the time.
I've added a few extra things here and there so this is not exactly what is required from the book.
Java Code:

Kotlin Code:

It's seems kind of weird for lack of a better term. In Java I can use crime.getId() from the Kotlin object, but I don't have that method defined in the Kotlin object.
On the https://try.kotlinlang.org site then have a Java to Kotlin conversion tool. However I suspect that there are limits as to what it can do.

I should point out that it's a one way conversion from Java to Kotlin right now. There are some Kotlin to Java converters out that, which people have had mixed results with.
 
Pete Letkeman
Saloon Keeper
Posts: 1692
68
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, do not ask me to explain the Kotlin code as I've spent only about ten minutes with it thus far.
I only really started looking into Kotlin on my free time thanks to this post by Liutauras Vilda:
https://coderanch.com/t/684277/Recording-Devoxx-UK-Kotlin-Ready
 
Pete Letkeman
Saloon Keeper
Posts: 1692
68
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, this is exciting and interesting to me.
I found this here: https://antonioleiva.com/data-classes-kotlin/
This one line of Kotlin code:

equals this in Java:

The reason my initial Kotlin code was 26 lines was because I converted my POJO object to a KOJO.
Had I first started with a KOJO I would have had to only write a few lines of code.

26 lines of code vs 88 lines code, not bad.
Less then 10 lines of code, with the extra constructors, compared to 88 lines or even 26 lines of code, even better.

A KOJO to POJO converter could be really helpful when starting out a Java project. Yes I know that there are code generators out there, but that is not quite the same as the way what Kotlin does it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!