Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

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

like image 892
Adit Gupta Avatar asked Mar 12 '13 18:03

Adit Gupta


People also ask

What is the maximum limit of string in Java?

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.

What is the maximum limit for the string value size?

While an individual quoted string cannot be longer than 2048 bytes, a string literal of roughly 65535 bytes can be constructed by concatenating strings.

How much text can a string hold?

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.

How many values can a string store?

A string can contain from 0 to approximately two billion (2 ^ 31) Unicode characters.


1 Answers

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.

like image 199
Raghav Sood Avatar answered Oct 11 '22 17:10

Raghav Sood