How Many Characters Can a Java String Have

How many characters can a Java String have?

You should be able to get a String of length

  1. Integer.MAX_VALUE always 2,147,483,647 (231 - 1)

    (Defined by the Java specification, the maximum size of an array, which the String class uses for internal storage)

    OR

  2. Half your maximum heap size (since each character is two bytes) whichever is smaller.

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

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.

how much size java string variable can hold

Java strings can only hold 2147483647 (2^31 - 1) characters (depending on your JVM). So if your string is bigger than that, you will have to break up the string. You could break up the string and store each value in an array, or a hash map, or any viable data structure.

String's Maximum length in Java - calling length() method

Considering the String class' length method returns an int, the maximum length that would be returned by the method would be Integer.MAX_VALUE, which is 2^31 - 1 (or approximately 2 billion.)

In terms of lengths and indexing of arrays, (such as char[], which is probably the way the internal data representation is implemented for Strings), Chapter 10: Arrays of The Java Language Specification, Java SE 7 Edition says the following:

The variables contained in an array
have no names; instead they are
referenced by array access expressions
that use nonnegative integer index
values. These variables are called the
components of the array. If an array
has n components, we say n is the
length of the array; the components of
the array are referenced using integer
indices from 0 to n - 1, inclusive.

Furthermore, the indexing must be by int values, as mentioned in Section 10.4:

Arrays must be indexed by int values;

Therefore, it appears that the limit is indeed 2^31 - 1, as that is the maximum value for a nonnegative int value.

However, there probably are going to be other limitations, such as the maximum allocatable size for an array.

Java with String length limit

Can anyone confirm if this is indeed a limitation of Java?

Yes. There is an implementation limit of 65535 on the length of a string literal1. It is not stated in the JLS, but it is implied by the structure of the class file; see JVM Spec 4.4.7 and note that the string length field is 'u2' ... which means a 16 bit unsigned integer.

Note that a String object can have up to 2^31 - 1 characters. The 2^16 -1 limit is actually for string-valued constant expressions; e.g. string literals or concatenations of literals that are embedded in the source code of a Java program.


If you want to a String that represents the first million digits of Pi, then it would be better to read the characters from a file in the filesystem, or a resource on the classpath.


1 - This limit is actually on the number of bytes in the (modified) UTF-8 representation of the String. If the string consists of characters in the range 0x01 to 0x7f, then each byte represents a single character. Otherwise, a character can require up to 6 bytes.

How many characters we can store in String datatype in java?

A little logic.

Java's string length returns int, so Integer max value might be the max size.

Limiting the number of characters in a string, and chopping off the rest

Use this to cut off the non needed characters:

String.substring(0, maxLength); 

Example:

String aString ="123456789";
String cutString = aString.substring(0, 4);
// Output is: "1234"

To ensure you are not getting an IndexOutOfBoundsException when the input string is less than the expected length do the following instead:

int maxLength = (inputString.length() < MAX_CHAR)?inputString.length():MAX_CHAR;
inputString = inputString.substring(0, maxLength);

If you want your integers and doubles to have a certain length then I suggest you use NumberFormat to format your numbers instead of cutting off their string representation.



Related Topics



Leave a reply



Submit