File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Maximum length of a string??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Maximum length of a string???" Watch "Maximum length of a string???" New topic

Maximum length of a string???

Gaurav Jain
Ranch Hand

Joined: Mar 11, 2004
Posts: 108
How much kb of data can i store in a string??
Is there any limit to it???
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
it has to be limited by Integer.MAX_VALUE as String's length() method returns int ...
I had this similar discussion sometime back here and I remember Jim explained it to me but I can't find that thread right now...
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
I believe a String's value is stored internally as a char[] array. Therefore, since you can't create a char[] array with a length > Integer.MAX_VALUE, a String must not be larger than Integer.MAX_VALUE.

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Gaurav Jain
Ranch Hand

Joined: Mar 11, 2004
Posts: 108
Thats a pretty subjective reply.
I am looking for an actual number.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Um, Integer.MAX_VALUE is an actual number. It's equal to 2147483647.
Now in practice that requires about 4 GB of memory (each char takes 2 bytes), so you'll probably get an OutOfMemoryError before you create a String that big, unless you've got lots of available RAM and the JVM is configured to use it. But that's an environment-dependent limitation, not absolute. Integer.MAX_VALUE is a fundamental limitation - there's no way you can circumvent it.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Maximum length of a string???
It's not a secret anymore!