I know this may be a really noobish qustion to ask but I found no good reliable resource where I can get this info.
In an Android application, I'm trying to hold data in a String by just continuing to append it with more data . And finally when the user closes the application or hits a "save button", I log it into a data .csv file.
I want to know how much data can a simple String hold in java?, So that the app does not crash if the String datatype runs out of the memory allocated. BTW I have tried to stress test my application, and it seems to run fine no matter how much data I store in one string.
Adit
String is considered as char array internally,So indexing is done within the maximum range. This means we cannot index the 2147483648th member.So the maximum length of String in java is 2147483647.
While an individual quoted string cannot be longer than 2048 bytes, a string literal of roughly 65535 bytes can be constructed by concatenating strings.
A character-string value is a sequence of characters. The number of characters in a sequence is called the length of the sequence. In Open PL/I, the maximum length of a string value is 32767 bytes or characters. A character-string of zero length is called a null string.
A string can contain from 0 to approximately two billion (2 ^ 31) Unicode characters.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With