• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Joe Boxer
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
just wondering if there is a maximum length of characters we can assign to a varaible of the type String?
cheers
Joe
 
Stefan Krompass
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
cu
Stefan
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Cheers,
--Tim
[ May 04, 2004: Message edited by: Tim West ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic