What is the maximum amount of data that a String can hold in java? [duplicate]

Seeing as the String class’ length() method returns an int value, the maximum length that would be returned by the method would be Integer.MAX_VALUE, which is 2^31 – 1 (or approximately 2 billion.)

So you can have a String of 2,147,483,647 characters, theoretically. I don’t think you should need much more than that.

However, as @TedHopp has pointed out in the comments and posted in his answer, the Android system limits your heap space, going as low as 16 MB. Therefore, you’ll never practically be able to reach the theoretical limit, and will max out with a String somewhere in the 4-64 million character range.

Leave a Comment