3 ms·
I found the transition from Java to Kotlin painless because Android Studio (Intellij) holds your hand all the way. If at first you're unsure of the syntax, you
by forfengeligfaen 8y ago
I found the transition from Java to Kotlin painless because Android Studio (Intellij) holds your hand all the way. If at first you're unsure of the syntax, you can just write code in Java and convert to Kotlin.
I agree that the null-safety Java interoperability does not work properly if the Java code is not annotated correctly (or you're parsing JSON) but I'd still prefer to have it than not.
I like Kotlin because:
* If is an expression and it feels so clean
* Functions are first-class (you can pass functions to functions)
* Kotlin is less verbose than Java so there is less to read and write
* You don't have to write "new"
* Constructors are cleaner (you don't have to write (this.a = a)
* The map function (on Android we were stuck on Java 7)
- kjeetgill 8y agoI'm unfamiliar with Kotlin in detail. Can you explain "Functions are first-class (you can pass functions to functions)"? Are they more powerful than Java's lambdas?
- forfengeligfaen 8y agoIn Java 7 (which is what I was using) you could not assign a method to a variable or pass it to another method. So I think it is more powerful. https://kotlinlang.org/docs/reference/lambdas.html https://kotlinlang.org/docs/reference/lambdas.html
- cmurdock 8y agoI think the big use-case is on Android where you're stuck with Java 7 and therefore can't use lambdas.
- dlubarov 8y agoThat used to be the case, but the build tools have supported lambdas for a while now, and it works even on very old devices. On the other hand, Java 8 classes like java.util.Stream only exist on API 24+ devices. So if you want to support older classes, you can't use the standard stream library.
- cmurdock 8y agoAh interesting. Thanks for the info.
- jrs95 8y agoYes, Java lambdas are just syntactic sugar over anonymous classes with a single method IIRC. You must define a full Interface to use when defining the function signature. Kotlin functions are essentially objects that can be passed around, stored in variables, etc. You can also have functions that don’t belong to any class, no need to use static methods for that.
- krzyk 8y agoNot exactly, lambdas use invokedynamic which has different performance characteristics when compared to anonymous classes. See a talk by Brian Goetz - https://www.youtube.com/watch?v=MLksirK9nnE https://www.youtube.com/watch?v=MLksirK9nnE
- jrs95 8y agoGreat, thank you for the correction. I only knew of what it looked like from an implementation perspective, had no idea things worked differently at runtime.
- guelo 8y agoThey're slightly different. Kotlin's compiler does some tricks that Java doesn't do to inline lambdas passed to functions. But Kotlin doesn't currently use Java 8's invokedynamic on the JVM, though they're planning to in the future. Not sure about on Dalvik.