Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is memory allocated for a string?

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.

like image 271
user1210233 Avatar asked Dec 16 '22 21:12

user1210233


1 Answers

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(...).

like image 197
Romain Avatar answered Dec 27 '22 12:12

Romain