Can a string contain more than Integer.MAX_VALUE number of characters

If I want to create a String object that contains x number of characters, where x> Integer.MAX_VALUE, can I do this?

Thanks.

+4
source share
3 answers

Look at the source.

the count field, which indicates the size of the int string, so you get an overflow.

 private final int count; 
+5
source

Instead of storing a single line 2 bits long (this will use 8 GB of memory to create bits). You can create a collection of strings. It is not so easy to work, but can be of any length effectively.

+3
source

Since String in java is a reference type, strings are stored in a contiguous block of memory. this block must be accessible by integer indices. The memory range must be between 0 and 2 ^ 32 -1 in the 32-bit computer architecture, which is equal to the int-primitive data type.

The main integer data type can access your memory range. Therefore, you cannot save a string that exceeds your memory.

In addition, you cannot store any data that exceeds your program stack, which is a very limited memory range compared to system memory. you will get a stackOverFlow exception when the application memory is exceeded.

+2
source

Source: https://habr.com/ru/post/1399648/


All Articles