This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes is there a maximum length for java.lang.String? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "is there a maximum length for java.lang.String?" Watch "is there a maximum length for java.lang.String?" New topic

is there a maximum length for java.lang.String?

Joe Boxer

Joined: Oct 23, 2003
Posts: 15
just wondering if there is a maximum length of characters we can assign to a varaible of the type String?
Stefan Krompass
Ranch Hand

Joined: Apr 29, 2004
Posts: 75
the boundary is 2^31-1 characters, since the getLength()-method of String returns an int-value. The other boundary is your RAM. So your String is limited by the boundary that is reached first.
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
There is one other limitation, though it would only come up rarely in practice: current Sun compilers don't like literal strings (like String s = "foo") over 65535 characters long.
This was discussed somewhere on JavaRanch a month or two back, I remember writing a quick program to confirm it.
In practical terms, RAM limitations crop up before int overflow errors. With repeated doubling of String length, think I crashed my JVM at length of around 2^23.
[ May 04, 2004: Message edited by: Tim West ]
Have you checked out Aspose?
subject: is there a maximum length for java.lang.String?
It's not a secret anymore!