如果我用 Java 写这个:
String s = "abc";
难道s在内存中只占3个字节吗?
如果为真,JVM如何找到String对象的结尾?
这会占用更多内存字节吗?
最佳答案
它需要超过 3 个字节,请继续阅读 this page 的完整解释
For reasons we'll explore below, the minimum memory usage of a Java String (in the Hotspot Java 6 VM) is generally as follows:
Minimum String memory usage (bytes) = 8 * (int) ((((no chars) * 2) + 45) / 8)
Or, put another way:
- multiply the number of characters of the String by two;
- add 38;
- if the result is not a multiple of 8, round up to the next multiple of 8;
- the result is generally the minimum number of bytes taken up on the heap by the String.
This page来自同一网站还详细介绍了使用不同于 String
关于java - s 在 Java 中占用 3 个字节吗?当: String s = "abc"; Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12447819/