A selection of some of the most frequent questions about Kotlin on Stack Overflow, asked, answered and explained. Something in there for everyone, from beginners to experts.
Array vs IntArray • IntArray Avoids boxing Easier to initialize • Array Might be required by an API val intArray = IntArray(10) val arrayOfInts = Array(5) { i -> i * 2 }
Array vs IntArray • IntArray Avoids boxing Easier to initialize • Array Might be required by an API Can store nullable values val intArray = IntArray(10) val arrayOfInts = Array(5) { i -> i * 2 } val notPeople: Array = arrayOfNulls(13)
Iterable vs Sequence • Iterable Use by default Usually the best choice • Sequence To handle an infinite number of elements For huge collections, judiciously
Iterable vs Sequence • Iterable Use by default Usually the best choice • Sequence To handle an infinite number of elements For huge collections, judiciously • Stream
Iterable vs Sequence • Iterable Use by default Usually the best choice • Sequence To handle an infinite number of elements For huge collections, judiciously • Stream When interoperating with Java
Iteration for (i in 0..args.size - 1) { println(args[i]) } for (i in 0..args.lastIndex) { println(args[i]) } for (i in 0 until args.size) { println(args[i]) }
Iteration for (i in 0..args.size - 1) { println(args[i]) } for (i in 0..args.lastIndex) { println(args[i]) } for (i in 0 until args.size) { println(args[i]) } for (i in args.indices) { println(args[i]) }
Iteration for (i in 0..args.size - 1) { println(args[i]) } for (i in 0..args.lastIndex) { println(args[i]) } for (i in 0 until args.size) { println(args[i]) } for (i in args.indices) { println(args[i]) } for (arg in args) { println(arg) }
Iteration for (i in 0..args.size - 1) { println(args[i]) } for (i in 0..args.lastIndex) { println(args[i]) } for (i in 0 until args.size) { println(args[i]) } for (i in args.indices) { println(args[i]) } for (arg in args) { println(arg) } args.forEach { arg -> println(arg) }
Iteration for (i in 0..args.size - 1) { println(args[i]) } for (i in 0..args.lastIndex) { println(args[i]) } for (i in 0 until args.size) { println(args[i]) } for (i in args.indices) { println(args[i]) } for (arg in args) { println(arg) } args.forEach { arg -> println(arg) }
Iteration for (i in 0..args.size - 1) { println(args[i]) } for (i in 0..args.lastIndex) { println(args[i]) } for (i in 0 until args.size) { println(args[i]) } for (i in args.indices) { println(args[i]) } for (arg in args) { println(arg) } args.forEach { arg -> println(arg) }
SAM conversions public interface OnClickListener { void onClick(Button button); } public class Button { public void setListener(OnClickListener listener) { ... } }
SAM conversions #2 public interface OnClickListener { boolean onClick(Button button); } button.setListener { println("Clicked!") } Kotlin: Type mismatch: inferred type is Unit but Boolean was expected
Function declaration reference Declaration Kotlin usage Java usage Companion object Foo.f() Foo.Companion.f(); Companion object with @JvmStatic Foo.f() Foo.f(); Object Foo.f() Foo.INSTANCE.f(); Object with @JvmStatic Foo.f() Foo.f(); Top level function f() UtilKt.f(); Top level function with @JvmName* f() Util.f(); * With the @JvmName annotation on the file use-site target
Variable declaration reference Declaration Kotlin usage Java usage Companion object X.x X.Companion.getX(); Companion object with @JvmStatic X.x X.getX(); Companion object with @JvmField X.x X.x; Companion object with const X.x X.x; Object X.x X.INSTANCE.getX(); Object with @JvmStatic X.x X.getX(); Object with @JvmField X.x X.x; Object with const X.x X.x; Top level variable x ConstKt.getX(); Top level variable with @JvmField x ConstKt.x; Top level variable with const x ConstKt.x; Top level variable with @JvmName* x Const.getX(); Top level variable with @JvmName* and @JvmField x Const.x; Top level variable with @JvmName* and const x Const.x; * With the @JvmName annotation on the file use-site target
Smart casts on mutable properties class Dog(var toy: Toy? = null) { fun play() { if (toy != null) { toy.chew() } } } Kotlin: Smart cast to 'Toy' is impossible, because 'toy' is a mutable property that could have been changed by this time
null!! class Episode { var airdate: Date = null!! } fun getWeekDay(date: Date?): WeekDay { return date!!.getWeekDay() } Date date as Date throw NullPointerException()
Platform types in overrides public interface OnClickListener { void onClick(Button button); } class KtListener : OnClickListener { override fun onClick(button: Button?): Unit { val name = button?.name ?: "Unknown button" println("Clicked ${name}") } }
Platform types in overrides public interface OnClickListener { void onClick(Button button); } class KtListener : OnClickListener { override fun onClick(button: Button?): Unit { val name = button?.name ?: "Unknown button" println("Clicked ${name}") } }
Platform types in overrides public interface OnClickListener { void onClick(Button button); } class KtListener : OnClickListener { override fun onClick(button: Button?): Unit { val name = button?.name ?: "Unknown button" println("Clicked ${name}") } }
Platform types in overrides public interface OnClickListener { void onClick(Button button); } class KtListener : OnClickListener { override fun onClick(button: Button): Unit { val name = button.name println("Clicked ${name}") } }
References • Top 10 Kotlin Stack Overflow questions, article series https://zsmb.co/top-10-kotlin-stack-overflow-questions-1/ • Kotlin tag on Stack Overflow https://stackoverflow.com/questions/tagged/kotlin