File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maximum length of a string???

 
Gaurav Jain
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How much kb of data can i store in a string??
Is there any limit to it???
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it has to be limited by Integer.MAX_VALUE as String's length() method returns int ...
I had this similar discussion sometime back here and I remember Jim explained it to me but I can't find that thread right now...
Regards
Maulin
 
Blake Minghelli
Ranch Hand
Posts: 331
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe a String's value is stored internally as a char[] array. Therefore, since you can't create a char[] array with a length > Integer.MAX_VALUE, a String must not be larger than Integer.MAX_VALUE.
 
Gaurav Jain
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats a pretty subjective reply.
I am looking for an actual number.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, Integer.MAX_VALUE is an actual number. It's equal to 2147483647.
Now in practice that requires about 4 GB of memory (each char takes 2 bytes), so you'll probably get an OutOfMemoryError before you create a String that big, unless you've got lots of available RAM and the JVM is configured to use it. But that's an environment-dependent limitation, not absolute. Integer.MAX_VALUE is a fundamental limitation - there's no way you can circumvent it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic