Kotlin String Templates

Introduces how to display variables and expressions inside string literals.

Inside a string literal, you can write "$variableName" or "${expression}" to expand the value of a variable or the result of evaluating an expression into the string.

val name = "devkuma"
println("Hello, $name!")

Output:

Hello, devkuma!

This structure is called a string template. By using "${expression}", you can also call functions inside a string template.

println("You are ${p.age} years old.")  // Calls getAge()

However, overly complex expressions reduce readability, so use them in moderation.

fun main(args: Array<String>) {
    println("Hi, ${if (args.isEmpty()) "anonymous" else args[0]}")
}