Basically for me less thinking required to write kotlin equivalent to most java code:
data class
- java: you have to write getters and setters for each thing, you have to write
hashCode
properly (or let IDE auto generate, which you have to do again every time you change the class),toString
(same problem ashashcode
) andequals
(same problem ashashCode
). or you could use lombok, but that comes with some quirky problems of its own.record
types are hopefully on the way. *kotlin:data class
does it all for you.
getter and setter patterns
- java: rewrite the getter and setter for each variable you use it for
- kotlin: don’t have to write getter and setter, and custom getter and setter take a lot less typing in kotlin if you do want to. also delegates exist for identical getters\setters
abstract
vs open
classes
- java: you have to make an abstract class implementation
- kotlin:
open class
lets you make an inheritable class while also being usable itself. nice mix of interface and regular class imo
extension functions
- java: doesnt exist
- kotlin: does exist, makes functions more clear in usage and feels more natural.
null
- java: Anything but primitives can be null at any time.
- kotlin: you get to decide what can and cant be null. allows for nice things like
inline class
singleton
- java: Memorize singleton pattern
- kotlin:
object
instead ofclass
generics
- java: Theyre alright, nothing fancy
- kotlin: Reified generics (you can access the actual type),
in
andout
for covariance
named parameters
- java: does not exist, easy to break api back-compatibility if you arent careful.
- kotlin: does exist, easy to preserve api back-compatiblity.
primary constructor
- java: does not have per-se, you still have to define everything inside the class
- kotlin: very nice to be able to quickly write a constructor without any constructor function or extra needless declarations