It's not a secret anymore!
The moose likes Beginning Java and the fly likes Length of a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Length of a String" Watch "Length of a String" New topic

Length of a String

Emil Karlsson
Ranch Hand

Joined: May 23, 2002
Posts: 63
Hi, a short question.
Is there any maxlength for a String?
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Yes, Integer.MAX_VALUE

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
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
As you can see Sun source of String (from
public final class String
implements, Comparable, CharSequence
/** The value is used for character storage. */
private char value[];
/** The offset is the first index of the storage that is used. */
private int offset;
/** The count is the number of characters in the String. */
private int count;
/** Cache the hash code for the string */
private int hash = 0;
for allocation characters used char array, so
max length of string is max length of array =
max int value 2**32 = 4GB, but as array index can be used only positive int, so it should be 2Gb.
Emil Karlsson
Ranch Hand

Joined: May 23, 2002
Posts: 63
Thanks a lot!
I agree. Here's the link:
subject: Length of a String
It's not a secret anymore!