What's the length of a string? [duplicate]
Possible Duplicate:
how many characters can a Java string开发者_运维百科 have?
How many characters can a string hold at it's maximum. (java)
heap memory is the limit OR the Integer.MAX_VALUE which ever is smaller.
A lot. Specifically, there does not seem to be any limit, though you will eventually out-of-memory. See the JVM spec for more info, or lack of info: http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#25486
EDIT: It looks like Java (the language) allows you to to make a string the length of any single variable, which is probably the amount of heapspace Java has allocated. (see the -Xmx argument).
In theory: 2^31 - 1 = 2147483647 (~2 GigaByte). In practice: end of virtual memory.
Since String
is based on an underlying char[]
, and array indices are int
values, the maximum length of a String
is Integer.MAX_VALUE
, though you'll probably run into memory issues before that.
精彩评论