Strings
String Fundamentals
Master string internal representations, immutability vs mutability, ASCII/Unicode encoding, and memory pool allocations.
Last Updated: August 2, 2026
•
20 min read
1. Introduction
What is a String?
A String is a sequence of characters stored in memory. In computer memory, every character is represented by an integer code point according to an encoding standard like ASCII or UTF-8/UTF-16.Why is it Important?
Strings are the primary format for human-readable data (text, JSON, URLs, user input). Because strings are immutable in languages like Java and Python, modifying a string repeatedly in a loop createsN temporary copies, turning an O(N) task into a slow O(N²) operation.
Where is it Used?
2. Mental Model
Imagine a string as an Inscribed Stone Tablet.
Once carved into stone, you cannot change a single letter in place. To change "Hello" to "Helps", you must carve a brand new stone tablet containing all 5 letters! In languages like Java and Python, strings behave exactly like these stone tablets (immutable). In C++, strings are mutable like whiteboards.
3. Concept: Immutability, Encodings & Memory
String Immutability
In Java and Python, String objects are immutable for security, thread safety, and memory caching (String Constant Pool).Character Encoding: ASCII vs Unicode
A-Z, a-z, 0-9, punctuation).'A' = 65, 'a' = 97, '0' = 48.
- Uppercase to lowercase conversion: 'a' = 'A' + 32.
4. Visuals
Memory Allocation: String Constant Pool (Java)
Character Encoding Summary
| Encoding | Bits per Char | Character Count | Use Case |
|---|---|---|---|
| ASCII | 7 / 8 bits (1 byte) | 128 (0 - 127) | Standard English text |
| UTF-8 | 8 to 32 bits (1 - 4 bytes) | 1,114,112 | Modern Web, Multi-lingual, Emojis |
| UTF-16 | 16 or 32 bits (2 or 4 bytes) | 1,114,112 | Java / C# internal char storage |
5. Real-World Examples
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers check whether you know how to convert characters to zero-indexed frequency array slots:int index = c - 'a'; (for lower-case English letters 'a' through 'z').
Common Mistakes
Warning: 1. Repeated String Concatenation (
+=) in Loops: Performing s += char inside an N-iteration loop creates N intermediate string objects, taking O(N²) time! Use StringBuilder in Java or ''.join(list) in Python.> 2. Comparing Strings with==in Java:==checks memory address reference equality, NOT content equality! Always use.equals()in Java.
7. Summary
+= inside loops is an O(N²) antipattern. Use StringBuilder / array join.'a'..'z' using c - 'a' enables O(1) lookup in a size-26 frequency array.8. Quiz
Question 1: What is the ASCII integer code for uppercase 'A' and lowercase 'a'?
Answer:'A' = 65, 'a' = 97 (difference of 32).
Question 2: What is the time complexity of building a string of length N by executing s += 'a' inside an N-step loop in Python?
Answer:O(N²), because each += creates a new copy of the string of length 1, 2, 3 ... N. Total work = \sum_{i=1}^N i = O(N²).
Question 3: Why does String immutability make multi-threaded programs safer?
Answer: Because immutable string instances cannot be modified by any thread, eliminating data races and thread synchronization locks during concurrent access.Question 4: How do you map a lowercase character c ('a'-'z') to an integer index between 0 and 25?
Answer:int index = c - 'a';
Question 5: In Java, what is the difference between String a = "test" and String b = new String("test")?
Answer:"test" is stored once in the String Constant Pool (shared reference). new String("test") explicitly creates a brand new object on the heap outside the pool.