String Value Semantics

Who changes after assignment — PHP string value semantics against string mutability in 10 languages.

C C23

C23
#include <stdio.h>
#include <string.h>

/* C strings are mutable char buffers with no value semantics: copying is
   a manual strcpy, and strcat writes in place */
int main(void) {
    char s[] = "hello";
    char t[16];
    strcpy(t, s);
    strcat(t, " world");
    printf("%s\n", s);
    printf("%s\n", t);
    return 0;
}
Output
hello
hello world

C++ C++26

C++26
#include <cstdio>
#include <string>

// std::string has value semantics: assignment builds an independent copy,
// so the original is untouched when the copy is modified
int main() {
    std::string s = "hello";
    std::string t = s;
    t += " world";
    std::printf("%s\n", s.c_str());
    std::printf("%s\n", t.c_str());
}
Output
hello
hello world

C# v11.0.100

v11.0.100
string s = "hello";
string t = s;   // the reference is copied, the object is not
string u = s.ToUpperInvariant(); // methods return new strings
Console.WriteLine(s);
Console.WriteLine(t);
Console.WriteLine(u);
t += " world";  // strings are immutable: a new object is built
Console.WriteLine(t);
Output
hello
hello
HELLO
hello world

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strings"
)

// Go strings are immutable values: assignment shares the backing pointer
// but concatenation always builds a fresh string
func main() {
	s := "hello"
	t := s
	t += " world"
	u := strings.ToUpper(s)
	fmt.Println(s)
	fmt.Println(t)
	fmt.Println(u)
}
Output
hello
hello world
HELLO

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        String s = "hello";
        String t = s;   // the reference is copied, the object is not
        String u = s.toUpperCase(); // methods return new strings
        System.out.println(s);
        System.out.println(t);
        System.out.println(u);
        t += " world"; // strings are immutable: a fresh object is built
        System.out.println(t);
    }
}
Output
hello
hello
HELLO
hello world

JavaScript v26.13.1

v26.13.1
const s = "hello";
const t = s;          // both point at the same immutable string
const u = s.toUpperCase(); // methods return new strings
console.log(s);
console.log(t);
console.log(u);
let r = s;
r += " world"; // a new string is built; s keeps the old one
console.log(r);
Output
hello
hello
HELLO
hello world

PHP v8.6.0

v8.6.0
// PHP strings are value semantics: assignment behaves as a full copy,
// so mutating one variable never shows through the other
$s = "hello";
$t = $s;          // an independent copy (COW makes it cheap until written)
$t .= " world";
echo $s, "\n";    // untouched

echo $t, "\n";

// string functions return new strings; the original stays put
$u = strtoupper($s);
echo $s, "\n";
echo $u, "\n";
Output
hello
hello world
hello
HELLO

Python v3.15.2

v3.15.2
s = "hello"
t = s          # both names point at the same immutable object
u = s.upper()  # methods return new strings
print(s)
print(t)
print(u)
r = s
r += " world" # a new object is built; s keeps the old one
print(r)
Output
hello
hello
HELLO
hello world

Rust v1.99.1

v1.99.1
// String owns its bytes; &str borrows a view. Assignment moves, so an
// independent copy must be requested with an explicit clone
fn main() {
    let s = String::from("hello");
    let mut t = s.clone();
    t.push_str(" world");
    println!("{}", s);
    println!("{}", t);
    let u = s.to_uppercase(); // returns a new String
    println!("{}", u);
}
Output
hello
hello world
HELLO

TypeScript v26.13.1

v26.13.1
const s: string = "hello";
const t = s;              // both point at the same immutable string
const u = s.toUpperCase(); // methods return new strings
console.log(s);
console.log(t);
console.log(u);
let r = s;
r += " world"; // a new string is built; s keeps the old one
console.log(r);
Output
hello
hello
HELLO
hello world

Language differences

C: mutable char buffers (example): a string is just a char array rewritten in place by strcpy/strcat — neither immutability nor value semantics; copying and concatenation are fully manual and the root of buffer-overflow bug classes.

C++: value semantics, mutable (example): assigning a std::string builds an independent copy (copy constructor) and mutating the copy never touches the original — the same semantics as PHP; implementations may apply SSO (short-string inlining) but the value semantics stand; C++17's string_view is the borrowed view (a close cousin of Rust's &str).

C#: immutable plus references (example): string is a reference type with immutable content — assignment shares the reference and any modification builds a new object; interning makes identical literals share instances; StringBuilder is the official exit for hot concatenation paths.

Go: immutable values (example): a string is an immutable pointer-plus-length view; assignment shares the backing bytes but they cannot be rewritten; concatenation always allocates a fresh string; []byte and string conversions copy explicitly.

Java: immutable plus interning (example): String objects are immutable and literals intern into a pool (identical literals are the same object under ==); concatenation is compiler-optimized to StringBuilder/invokedynamic; the security model (String as lock, path, or class name) leans on immutability.

JavaScript: immutable with engine interning (example): string primitives are immutable and assignment shares the same object; mutating methods return new strings; engines internalize identical strings; concatenation rides rope/cons-cell optimizations with no StringBuilder needed.

PHP: value semantics (example): assignment behaves as a full independent copy (the engine's zend_string COW shares by reference count and separates only on write — semantics decoupled from performance); string functions uniformly return new strings; no interning (literals share at compile time, runtime concatenations each allocate); strings are byte strings with no encoding semantics.

Python: immutable (example): str objects are immutable and assignment shares the same object; mutating methods return new strings; small-string interning happens automatically for identifier-shaped literals; build strings with list join or io.StringIO.

Rust: owned vs borrowed (example): String owns its heap bytes and is mutable; &str is an immutable borrowed view — ownership makes a shared mutable string fail to compile; assignment moves (the original goes out of scope) and an independent copy needs an explicit clone; to_uppercase and friends return a new String.

TypeScript: at the language level the same as JS (example): immutable string primitives with shared references; at the type level string is a primitive (literal types act as singleton types: type Dir = "asc" | "desc"), and readonly can freeze object shapes while strings were already immutable.