String
01. What is a String?
A String is a contiguous sequence of characters used to represent textual data. It is one of the most widely used data structures in application software, mapping binary byte arrays into human-readable letters, numbers, and symbols.
Depending on the programming language, strings can be implemented as immutable primitives(e.g., Python, Java, JavaScript) or mutable character arrays (e.g., C, C++).
02. Key Characteristics
- Zero-Based Indexing: Individual characters are accessed via indexes starting from
0up tolength - 1. - Immutability vs. Mutability: Immutable strings cannot be changed after creation. Any modification operation (like concatenation) actually allocates and returns a completely new string in memory. Mutable strings can have characters swapped in-place.
- Null-Termination (Low Level): Traditional systems (like C) demarcate the end of a string by placing a null byte (
\0, integer value 0) in memory directly after the last character. - Character Encoding: Determines how binary bytes translate to character glyphs (e.g., ASCII for basic English characters, or UTF-8/UTF-16 for global Unicode symbols).
03. How Languages Represent It
Underneath the surface, strings are arrays of integers mapping to character codes:
['H', 'i', '\0'] ➔ [72, 105, 0] (3 bytes)- Object-style: Stores a length header (e.g.,
2) + pointer to character buffer.In modern languages, characters are represented using Unicode, which maps letters across all international alphabets and emojis.
04. Language Comparison
How different languages approach string primitives:
- Python: Uses the
strtype, which is immutable and represents Unicode sequences. Python has no separate character type (a single character is just a string of length 1). - Java: Standard strings are objects of the
Stringclass. They are immutable and cached in a special memory region called the String Constant Pool. Java uses a separatecharprimitive for single characters. - JavaScript: Strings are immutable primitives. Characters are stored as UTF-16 code units.
- C: Has no native string object. A C-string is a character array (
char[]) ending with a null character (\0). It is mutable in-place. - C++: Provides the
std::stringclass, which is a mutable, dynamic container wrapping raw character arrays.
05. Code Examples
Select a tab below to see code declarations and usage:
# In Python, strings are immutable text sequences
name = "Alice"
greeting = 'Hello' # Single or double quotes are equivalent
print(type(name)) # Output: <class 'str'>
print(len(name)) # Output: 5
# Indexing and Slicing (accessing characters/substrings)
first_char = name[0] # "A"
substring = name[1:4] # "lic" (indexes 1, 2, 3)
# String Concatenation (creates a new string)
full_msg = greeting + ", " + name
print(full_msg) # Output: "Hello, Alice"06. Key Differences
A structural comparison of string behaviors:
| Behavior | Python / Java / JS | C / C++ |
|---|---|---|
| Mutability | Strictly Immutable (creates new instances) | Mutable (characters can be updated in-place) |
| Termination Type | Length-prefixed (size is tracked explicitly) | Null-terminated (C-style \0) or object-managed |
| Memory Location | Heap object or String Pool | Stack, Heap, or Read-Only Data Segment |
07. Common Mistakes
1. Comparing Object Equality vs. Value Equality
In Java, comparing strings using the equality operator (name == "Alice") compares thememory references, not their actual characters. To compare the letters, you must use.equals(): name.equals("Alice").
2. Buffer Overflow / Missing Null-Terminator
In C, character arrays must include room for the null-terminator. Writing char name[5] = "Alice"; is a bug because "Alice" requires 6 bytes (5 letters + \0). Missing the terminator causes functions likeprintf or strlen to read past the array boundary into unrelated memory, causing crashes or security leaks.
3. Inefficient Loops (Concatenation Pitfall)
Because strings in languages like Java and Python are immutable, executing str += "x" inside a large loop continuously allocates and copies new string objects. This leads to O(N^2) time complexity. Use dynamic collections like StringBuilder in Java or join list segments in Python for optimal speed.