So, for the premise, what we’re talking about here is how big a String can be, how many bytes a String can have. If you think about the number of characters, you need to think about the character and byte mapping.

Let me summarize here, two cases:

If the String is created dynamically in code, it is in the heap

  • String is a char array whose length is an Int, so the longest String is <= integer.max_value (65535).
  • MAX_VALUE-8 =Integer.MAX_VALUE-8 =Integer
  • However, creating an array int [integer.max_value -8] will still exceed the heap’s memory, so the longest String is < integer.max_value -8

If String is constant, it is on the stack

Class describes the data structure of the global String variable, integer.max_value (65535). MAX_VALUE = Integer.MAX_VALUE = Integer.MAX_VALUE = Integer.