If I write this in Java:
String s = "abc";
Does s only use 3 bytes in memory?If true, how does the JVM find the end of a String object?Does it take up more bytes in memory?
It takes more than 3 bytes to get a full explanation from this page.
For the reasons we will discuss below, the minimum memory usage of a Java String (in Hotspot Java 6 VM) usually looks like this:Minimum String memory usage (bytes) = 8 * (int) ((((no chars) * 2) + 45) / 8) Or, in another way:multiply the number of characters of the string by two;add 38;if the result is not a multiple of 8, round to the next multiple of 8;- , , , .
For the reasons we will discuss below, the minimum memory usage of a Java String (in Hotspot Java 6 VM) usually looks like this:
Minimum String memory usage (bytes) = 8 * (int) ((((no chars) * 2) + 45) / 8)
Or, in another way:
, String
String
: , . JVM- , ; , . UTF-16, " 6 ?".: -)
JVM . .
. , .. , , , , , , substring.
substring
, : 3 .
Minimum string memory usage (bytes) = 8 * (int) (((((number of characters) * 2) + 45) / 8)