String interpolation

Unlike Java, Kotlin supports string interpolation where an actual variable, an expression value, can be used to substitute placeholders ($ sign followed by the variable name or $ sign followed by curly braces) inside string objects. This eliminates the need for memory-expensive string concatenation using the + operator in Java. This is explained using the following code:

var sum: Double = calculator.add(1.0, 2.0);
println("Double Sum of 1.0 + 2.0 = $sum");

...
println("No of times methods invoked in StringFormatter = ${stringFormatter.noOfTimesMethodsInvoked}");

In the preceding code snippet println, which is passed with string objects containing the placeholders $sum and ${stringFormatter.noOfTimesMethodsInvoked} respectively. Kotlin will replace the actual values with the placeholders before they're sent to be printed on the console.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.186.143