Bird
Raised Fist0
Kotlinprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between string concatenation and string templates in Kotlin?
easy
A. Templates require explicit conversion of variables to strings.
B. Concatenation uses + to join strings; templates insert variables with $ inside strings.
C. Concatenation is faster than templates in all cases.
D. Concatenation can only join strings, templates can join numbers only.

Solution

  1. Step 1: Understand string concatenation

    Concatenation joins strings using the + operator and requires converting non-string variables to strings explicitly.
  2. Step 2: Understand string templates

    Templates allow inserting variables directly inside strings using the $ symbol, making code cleaner and easier.
  3. Final Answer:

    Concatenation uses + to join strings; templates insert variables with $ inside strings. -> Option B
  4. Quick Check:

    Concatenation vs templates = C [OK]
Hint: Templates use $ for variables; concatenation uses + [OK]
Common Mistakes:
  • Thinking templates need explicit conversion
  • Believing concatenation is always faster
  • Confusing syntax of templates and concatenation
2. Which of the following is the correct Kotlin syntax to insert a variable name into a string using templates?
easy
A. "Hello, {name}!"
B. "Hello, \$name!"
C. "Hello, ${name}!"
D. "Hello, " + name + "!"

Solution

  1. Step 1: Recall string template syntax

    In Kotlin, to insert a variable inside a string, use $variable or ${variable} if expression is needed.
  2. Step 2: Identify correct usage for variable insertion

    For simple variables, $name works, but to be safe and clear, especially in complex expressions, ${name} is preferred.
  3. Final Answer:

    "Hello, ${name}!" -> Option C
  4. Quick Check:

    Variable insertion = ${name} [OK]
Hint: Use ${variable} for safe template insertion [OK]
Common Mistakes:
  • Using curly braces without $
  • Using + inside templates
  • Forgetting $ before variable
3. What will be the output of the following Kotlin code?
val age = 25
println("Age: " + age + " years")
println("Age: $age years")
medium
A. Age: 25 years Age: 25 years
B. Age: 25 years Age: $age years
C. Age: age years Age: 25 years
D. Age: 25 years Age: age years

Solution

  1. Step 1: Evaluate first println with concatenation

    "Age: " + age + " years" converts age (25) to string and concatenates, resulting in "Age: 25 years".
  2. Step 2: Evaluate second println with template

    "Age: $age years" inserts the value of age (25) directly, producing "Age: 25 years".
  3. Final Answer:

    Age: 25 years Age: 25 years -> Option A
  4. Quick Check:

    Concatenation and template output same = A [OK]
Hint: Templates insert values directly; concatenation converts explicitly [OK]
Common Mistakes:
  • Expecting $age to print literally in templates
  • Confusing variable name with value
  • Missing conversion in concatenation
4. Identify the error in this Kotlin code snippet:
val score = 90
println("Your score is: $score") + " points"
medium
A. The + operator is used incorrectly outside println.
B. Variable score should be inside curly braces ${score}.
C. Missing semicolon after println statement.
D. String templates cannot be combined with + operator.

Solution

  1. Step 1: Analyze the println statement

    println("Your score is: $score") prints the string with the variable inserted correctly.
  2. Step 2: Check the use of + operator

    The + " points" is outside the println call, so it tries to add a string to the Unit result of println, causing an error.
  3. Final Answer:

    The + operator is used incorrectly outside println. -> Option A
  4. Quick Check:

    Operator placement error = B [OK]
Hint: Ensure + is inside println or use separate print [OK]
Common Mistakes:
  • Thinking templates need curly braces always
  • Adding + after println call
  • Expecting semicolon in Kotlin
5. You want to create a message showing a user's name and age using Kotlin string templates. The variables are val name = "Anna" and val age = 30. Which code snippet correctly produces the output Hello, Anna! You are 30 years old.?
hard
A. println("Hello, " + name + "! You are " + age + " years old.")
B. println("Hello, $name! You are $age years old.")
C. println("Hello, ${name}! You are ${age} years old.")
D. Both B and C

Solution

  1. Step 1: Check concatenation approach

    println("Hello, " + name + "! You are " + age + " years old.") uses concatenation with + operators and works but is less clean.
  2. Step 2: Check template approaches

    String templates using $variable and ${variable} syntax are both correct: simple $variable vs ${variable}, both valid and produce the same output.
  3. Step 3: Determine correct options

    Since both string template syntaxes produce the correct output, the option indicating both are correct is the answer.
  4. Final Answer:

    Both B and C -> Option D
  5. Quick Check:

    Templates with $ or ${} = D [OK]
Hint: Use $var or ${var} for templates; both work [OK]
Common Mistakes:
  • Thinking only one template syntax is correct
  • Confusing concatenation with templates
  • Missing quotes around strings
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard