• Post Reply Bookmark Topic Watch Topic
  • New Topic

Length of a String  RSS feed

 
Varun Narula
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there some way to have more than the 65k chars a String can hold?
 
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings can hold a lot more than 65k characters - (there's an urban myth that String literals that are restricted to max 64k). A String object's length is only restricted by the value of Integer.MAX_VALUE (since that will be the biggest value that String.length() can return) - which is 2147483647. So a String can hold 2147483647 chars. Whether you should ever create a String which will use that much memory however is entirely another matter.
[ March 19, 2004: Message edited by: eammon bannon ]
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by eammon bannon:
Strings can hold a lot more than 65k characters - (there's an urban myth that String literals that are restricted to max 64k).
[ March 19, 2004: Message edited by: eammon bannon ]

Mmmm.....try this code

You will get ClassFormatError (Illegal constant pool type) when the class is loaded.
But this will work fine,

Got it?
 
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected Mani. That will teach me for believing what I read without trying it myself. Oh well. Still, its a pretty daft thing to want do in your code.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to see how many times I could double the length of a string and I got to a count of 23 with about 8 Meg before the operation failed with an out of memory error out of heap space which crashed the program (gracefully).
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An addendum to Eddie's post (had to try it for myself just to check) - the following worked fine.

If you uncomment the println(s) line, it will work still, but you get a whole lot of *s on your output
Mine also bombs out at 2^23.
Looks like it's a bug in the parser - can't have string literals more than 65536 chars?
--Tim
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What JDK version, patchlevel and platform are you using?
Geoffrey
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if that question was directed at me (it probably wasn't), but here goes...
The (run-time) error I get when a declaring a String literal with exactly 65536 '*'s (ie, String s = "***..." is (from a class 'DateTrial', I was messing around with dates earlier...):

My System properties are (System.getProperties().list(System.out); - hope this has enough info for you)

Incidentally, I was being daft earlier - it's not a bug in javac since it's a runtime error. I didn't notice as I was using compile/run in my IDE...
I'd post the code that produces the error, but it wouldn't be the prettiest of posts - one whole load of *s.
Cheers,

--Tim
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!