• Post Reply Bookmark Topic Watch Topic
  • New Topic

Big Java Strings  RSS feed

 
Randy Dizitser
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a string thats larger then the maximum size that the String class can accomodate. Is there a way to still store my string as one string?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Randy,

The String class ought to work with up to 2x10^31 characters, or about 4GB of data (each character is two bytes.) Most JVMs can't have a Java heap even half that size, so the practical limit is about 1800MB. How are you running into this limit? Maybe your application design could be changed?
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try appending to a StringBuffer object; but like Ernest said, maybe there is a neater way of doing what you want without building an enormous String?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody on java.lang.programmer spotted what is probably the misunderstanding here. A Java String literal constant is restricted in size because of limits on the class file structure. If you want a really huge String, why not read it from a file? Don't try to read line by line, but allocate a char[] of the correct size and read into that. Even better, if your text is all ascii, allocate a byte[] - taking half the space.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!