June 2018
Intermediate to advanced
316 pages
6h 34m
English
Strings are immutable, and whenever you try to modify an existing object, a new instance of the String class is created. The following example demonstrates this:
fun main(vars: Array<String>) { val cat1 = "Cat" val cat2 = cat1.plus("Dog") println(cat1) println(cat2) println(cat1 === cat2) println(cat1 === cat1)}
Here's the output:
CatCatDogfalsetrue
So it's bad practice to use concatenation:
val query = "SELECT id, firstName, lastName FROM Building " + building + " WHERE firstName = " + user.firstNameInstead, you should use the string templates feature:
val query = "SELECT id, firstName, lastName FROM Building $building WHERE firstName = ${user.firstName}"
Under the hood, the string templates feature uses the StringBuilder ...
Read now
Unlock full access