▲ | bsder 5 days ago | |||||||||||||||||||||||||||||||||||||||||||
It's not like C++ users don't say the exact same thing about String, though. The problem is that String really isn't a language primitive. Different people and programs always have a different notion of what a String should do (Should it be mutable? Should it always be valid UTF-8? Which operations should be O(1), O(n) or O(n log n)? etc.) | ||||||||||||||||||||||||||||||||||||||||||||
▲ | scottlamb 5 days ago | parent [-] | |||||||||||||||||||||||||||||||||||||||||||
> It's not like C++ users don't say the exact same thing about String, though. If they do, they're wrong, as the two languages are quite different here. In Java, String requires two allocations: your variable is implicitly a pointer to String allocation, which in turn has a pointer to a char[] allocation. In C++, the std::string itself is a value type. The actual bytes might be inline (short string optimization) or behind a single allocation accessible from the std::string. Rust's std::string::String is somewhere between: it's a value type but does not have a short string optimization (unless you count the empty string returned by String::new). > Different people and programs always have a different notion of what a String should do (Should it be mutable? Should it always be valid UTF-8? Which operations should be O(1), O(n) or O(n log n)? etc.) Sure, there can be call for writing your own String type. But what's unique about Java as compared to say C, C++, Go, Rust, even to some extent C# is that you can't have a class or struct that bundles up the parts of your data structure (in the case of a mutable string, two fields: data pointer/capacity + the used length) without boxing. There's a heavy cost to any non-primitive data type. | ||||||||||||||||||||||||||||||||||||||||||||
|