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 max size 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 "max size of a string" Watch "max size of a string" New topic

max size of a string

sitaram irrinki
Ranch Hand

Joined: Feb 16, 2005
Posts: 158
what is the maximum size of the string
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
There is an interesting newsletter on this and related stuff at the Maximum Solutions website, which publishes the Java Specialists newsletter.

Relevant issue:
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
That article is interesting, but I don't see that it addresses the original question.

As far as I know, the runtime size of String objects is only limited by the length field, which is an int.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
I might add to Ilja's comment that you are likely to see OutOfMemoryException before you can use the String approaching the int maximum size. Internally, the Strings are stored as arrays of chars, so even if one char is just one byte, and your machine memory is 1Gb, then the String of size 1 billion is your limit.

What is it that you are trying to code?
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
There is also another limitation - the size of a literal string variable in a Java file. This was discussed ages ago - some time early last year, perhaps even in this forum.

Anyway, at the time I found that if you declared a String viz:

This would compile OK, but

The compiler would die with a random error. From memory it was Sun's javac v1.4.x

That said, this is of no relevance to everyday coding...

I agree. Here's the link:
subject: max size of a string
It's not a secret anymore!