Does it get 3 bytes in Java? when: String s = "abc"; Java

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?

+5
source share
3 answers

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;
  • - , , , .

, String

+7

: , . JVM- , ; , . UTF-16, " 6 ?".: -)

JVM . .

. , .. , , , , , , substring.

, : 3 .

+2

Minimum string memory usage (bytes) = 8 * (int) (((((number of characters) * 2) + 45) / 8)

+1
source

All Articles