Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can String hold greater than Integer.MAX_VALUE number of character

Tags:

java

string

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

Thanks.

like image 238
Tapas Bose Avatar asked Mar 04 '12 14:03

Tapas Bose


2 Answers

Have a look at the source.

the field count, which indicates the string's size is an int - so you will get an overflow.

private final int count;
like image 143
amit Avatar answered Oct 03 '22 09:10

amit


Instead of storing a single String of length 2 bn (This will use 8 GB of memory to create btw) You can create a collection of Strings. Its not as easy to work with but can effectively be any length.

like image 34
Peter Lawrey Avatar answered Oct 03 '22 10:10

Peter Lawrey