January 2018
Intermediate to advanced
434 pages
14h 1m
English
Swapping two numbers is one of the most common things you do in programming. Most of the approaches are quite similar in nature: Either you do it using a third variable or using pointers.
In Java, we don't have pointers, so mostly we rely on a third variable.
You can obviously use something as mentioned here, which is just the Kotlin version of Java code:
var a = 1var b = 2run { val temp = a; a = b; b = temp }println(a) // print 2println(b) // print 1
However, in Kotlin, there is a very quick and intuitive way of doing it. Let's see how!
Read now
Unlock full access