Understanding String Type and Immutability in Kotlin
📖 Scenario: Imagine you are creating a simple program that stores a user's name and tries to change it. This will help you understand how strings work in Kotlin and why they cannot be changed once created.
🎯 Goal: You will create a string variable, try to change it, and then print the results to see how string immutability works in Kotlin.
📋 What You'll Learn
Create a string variable with a specific value
Create a new string by modifying the original string
Understand that the original string does not change
Print both the original and the new string
💡 Why This Matters
🌍 Real World
Understanding string immutability helps when working with user input, text processing, and data that should not change accidentally.
💼 Career
Many programming jobs require handling text data safely and efficiently, and knowing string immutability prevents bugs and improves code quality.
Progress0 / 4 steps
1
Create a string variable
Create a string variable called originalName and set it to the value "Alice".
Kotlin
Hint
Use val to create a string variable and assign the value with double quotes.
2
Create a new string by adding text
Create a new string variable called newName by adding the text " Smith" to originalName.
Kotlin
Hint
Use the + operator to join strings in Kotlin.
3
Show that original string is unchanged
Use println to print originalName and newName on separate lines.
Kotlin
Hint
Use println twice to print each string on its own line.
4
Observe string immutability
Run the program and observe that originalName remains "Alice" while newName is "Alice Smith". This shows strings cannot be changed once created.
Kotlin
Hint
The output shows the original string did not change after adding text.
Practice
(1/5)
1. What does it mean that a String in Kotlin is immutable?
easy
A. Strings can only hold numbers, not letters.
B. You can change characters inside the String directly.
C. Once created, the text inside a String cannot be changed.
D. Strings automatically update when variables change.
Solution
Step 1: Understand immutability concept
Immutability means the object cannot be changed after creation.
Step 2: Apply to Kotlin String
Kotlin Strings cannot be modified once created; any change creates a new String.
Final Answer:
Once created, the text inside a String cannot be changed. -> Option C
Quick Check:
String immutability = cannot change text [OK]
Hint: Remember: Strings are like printed paper, not editable [OK]
Common Mistakes:
Thinking you can change characters inside the same String
Confusing immutability with variable reassignment
Believing Strings hold only numbers
2. Which of the following is the correct way to concatenate two Strings str1 and str2 in Kotlin?
easy
A. str1 & str2
B. str1 + str2
C. str1.join(str2)
D. str1.append(str2)
Solution
Step 1: Recall Kotlin String concatenation syntax
Kotlin uses the + operator to join two Strings.
Step 2: Check each option
Only str1 + str2 is valid Kotlin syntax for concatenation.
Final Answer:
str1 + str2 -> Option B
Quick Check:
Use + to join Strings [OK]
Hint: Use + to combine Strings, like adding words together [OK]
Common Mistakes:
Using methods like append or join which don't exist on Kotlin String
Using & which is not a Kotlin operator for Strings
Trying to modify Strings directly
3. What will be the output of this Kotlin code?
val text = "Hello"
val newText = text + " World"
println(text)
medium
A. Hello
B. Hello World
C. World
D. HelloWorld
Solution
Step 1: Understand String immutability and concatenation
The original String text remains unchanged after concatenation.
Step 2: Analyze the print statement
Only text is printed, which is still "Hello".
Final Answer:
Hello -> Option A
Quick Check:
Original String unchanged = Hello [OK]
Hint: Printing original String shows it stays the same [OK]
Common Mistakes:
Assuming original String changes after + operation
Confusing newText with text in print
Ignoring immutability of Strings
4. Identify the error in this Kotlin code snippet:
var greeting = "Hi"
greeting[0] = 'B'
println(greeting)
medium
A. println cannot print Strings.
B. Variable greeting should be val, not var.
C. Single quotes cannot be used for characters.
D. You cannot change characters in a String by index.
Solution
Step 1: Check String mutability rules
Kotlin Strings are immutable; you cannot assign to an index.
Step 2: Analyze the code line causing error
Line greeting[0] = 'B' tries to change a character, which is invalid.
Final Answer:
You cannot change characters in a String by index. -> Option D
Quick Check:
Strings immutable = no index assignment [OK]
Hint: Strings are like frozen text, can't change letters inside [OK]
Common Mistakes:
Thinking var allows character change inside String
Confusing char literals with strings
Believing println can't print Strings
5. Given a list of words, how can you create a new list with each word appended by "!" without changing the original words?
val words = listOf("cat", "dog", "bird")
val excitedWords = ???
Which Kotlin code correctly does this considering String immutability?
hard
A. val excitedWords = words.map { it + "!" }
B. val excitedWords = words.apply { this[0] += "!" }
C. val excitedWords = words.forEach { it + "!" }
D. val excitedWords = words.replace("", "!")
Solution
Step 1: Understand immutability and list transformations
Strings cannot be changed, so create new Strings with + "!".
Step 2: Use map to transform each word into a new String
map returns a new list with transformed elements without modifying original.
Final Answer:
val excitedWords = words.map { it + "!" } -> Option A
Quick Check:
Use map + + operator for new list [OK]
Hint: Use map to create new list with modified Strings [OK]
Common Mistakes:
Trying to modify list elements directly with apply or forEach
Using replace on list instead of Strings
Assuming original list changes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.