Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

max size of a string

 
sitaram irrinki
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the maximum size of the string
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an interesting newsletter on this and related stuff at the Maximum Solutions website, which publishes the Java Specialists newsletter.

Website: www.javaspecialists.co.za
Relevant issue: www.javaspecialists.co.za/archive/Issue059.html
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...


-Tim
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic