How is memory allocated for a string?

c++, java, memory-management, string

Solution

In Java, `String` is an immutable Object, so the "size" of the `String` has to be known at time of allocation. It'll end up allocated in a shared object pool if it's "static" (e.g. a `String` litteral, like `"Hey, I'm a String litteral!"`), or on the heap if it's constructed using `new String(...)`.

Problem

How is memory allocated for a string, say in Java or C++? This might be silly, so please excuse me. I'm wondering because a string is of unknown size.

Original source