0
0
Kotlinprogramming~10 mins

String concatenation vs templates in Kotlin - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - String concatenation vs templates
Start
Define variables
Choose method
Concatenate
Create final string
Print or use string
End
This flow shows how Kotlin creates strings either by joining pieces manually (concatenation) or by embedding variables directly inside a template.
Execution Sample
Kotlin
val name = "Anna"
val age = 30
val concat = "Name: " + name + ", Age: " + age
val template = "Name: $name, Age: $age"
println(concat)
println(template)
This code shows two ways to make a string with variables: joining parts with + and using a template with $.
Execution Table
StepActionExpression EvaluatedResultOutput
1Define namename = "Anna"name = "Anna"
2Define ageage = 30age = 30
3Concatenate strings"Name: " + name + ", Age: " + age"Name: Anna, Age: 30"
4Create template string"Name: $name, Age: $age""Name: Anna, Age: 30"
5Print concatenated stringprintln(concat)Name: Anna, Age: 30
6Print template stringprintln(template)Name: Anna, Age: 30
💡 All steps complete, both strings created and printed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
nameundefined"Anna""Anna""Anna""Anna""Anna"
ageundefinedundefined30303030
concatundefinedundefinedundefined"Name: Anna, Age: 30""Name: Anna, Age: 30""Name: Anna, Age: 30"
templateundefinedundefinedundefinedundefined"Name: Anna, Age: 30""Name: Anna, Age: 30"
Key Moments - 2 Insights
Why do both concat and template have the same final string even though they look different?
Both methods combine the variable values with text. Concatenation joins pieces with +, while templates insert variables directly. Execution table rows 3 and 4 show both produce the same string.
What happens if you forget to use $ in the template string?
Without $, the variable name is treated as plain text, not replaced. So the output would show "name" literally. This is shown in step 4 where $name is replaced correctly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'concat' after step 3?
A"Name: Anna, Age: $age"
B"Name: $name, Age: $age"
C"Name: Anna, Age: 30"
D"Name: $name, Age: 30"
💡 Hint
Check the 'Result' column in row 3 of the execution table.
At which step is the template string created with variables replaced?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Action' and 'Expression Evaluated' columns in the execution table.
If you change 'age' to 31, which variable's value changes in variable_tracker after step 2?
Aage
Bname
Cconcat
Dtemplate
💡 Hint
Check the 'age' row in variable_tracker after step 2.
Concept Snapshot
Kotlin strings can be made by concatenation or templates.
Concatenation uses + to join strings and variables.
Templates use $variable inside quotes to insert values.
Templates are cleaner and easier to read.
Both produce the same final string output.
Full Transcript
This visual trace shows how Kotlin creates strings using two methods: concatenation and templates. First, variables 'name' and 'age' are defined. Then, 'concat' is made by joining strings and variables with +. Next, 'template' is created by embedding variables directly inside a string using $ syntax. Both strings end up the same. The execution table shows each step, including printing the strings. The variable tracker shows how values change after each step. Key moments clarify why both methods produce the same result and the importance of $ in templates. The quiz tests understanding of variable values and steps.