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.

Leave a Comment