File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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 ]
I agree. Here's the link:
subject: is there a maximum length for java.lang.String?
Similar Threads
Length of URL in get method
maximum length of String = 32k ?
maximum length of a string?
Max length of select statement
"packet length" or "captured length" in "Jpcap" library !?