0
0
Kotlinprogramming~15 mins

String concatenation vs templates in Kotlin - Hands-On Comparison

Choose your learning style9 modes available
String concatenation vs templates
📖 Scenario: You are creating a simple program to greet users by their names and ages. You want to see two ways to build the greeting message: one by joining strings manually and another by using Kotlin's string templates.
🎯 Goal: Build a Kotlin program that creates a greeting message using string concatenation and then using string templates. Finally, print both messages to compare.
📋 What You'll Learn
Create variables for name and age with exact values
Create a greeting message using string concatenation
Create a greeting message using string templates
Print both greeting messages
💡 Why This Matters
🌍 Real World
Building user-friendly messages in apps, like greetings, notifications, or reports, often requires combining text with variable data.
💼 Career
Knowing how to create readable and maintainable strings is essential for software developers, especially when working on user interfaces or generating dynamic content.
Progress0 / 4 steps
1
Create variables for name and age
Create a String variable called name with the value "Alice" and an Int variable called age with the value 30.
Kotlin
Need a hint?

Use val to create variables and assign the exact values.

2
Create greeting message using string concatenation
Create a String variable called greetingConcat that uses string concatenation with name and age to form the message: Hello, my name is Alice and I am 30 years old.
Kotlin
Need a hint?

Use the + operator to join strings and variables.

3
Create greeting message using string templates
Create a String variable called greetingTemplate that uses Kotlin string templates with name and age to form the same message: Hello, my name is Alice and I am 30 years old.
Kotlin
Need a hint?

Use $variableName inside the string to insert variable values.

4
Print both greeting messages
Print the variables greetingConcat and greetingTemplate each on its own line using println.
Kotlin
Need a hint?

Use println(greetingConcat) and println(greetingTemplate) to show the messages.