File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there any limit on the length of Strings...

 
Jay Damon
Ranch Hand
Posts: 282
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any limit on the length of Strings that may be used as a key value into a Map? I would to like to cache some data based on a complex SQL where clause criteria using the where clause as the key. The where clause may exceed 1000 characters but I don't expect it to get much larger than that.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there's no special limit.

If you want me to be extremely precise, I should say there are no special limits, aside from the intrinsic limits of the String class: a maximum of 2^31-1 characters, I believe.
 
Adam Nace
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a HashMap treats a String as an Object. Hence, ANY String will do, regardless of the length of the string, because ALL Strings, just like ALL Objects, have hashCodes and .equals methods.

One thing you should note, however, is that the .equals operation is O(n) on the size of the String, so the bigger the strings are, the longer it takes to do a compare on .equals.

Also, depending on how well distributed your String hashCodes are (they actually have a pretty good hash method now, if I'm not mistaken, but prior to 1.2, it was TERRIBLE, because it only examined the first 16 characters of the String -- in your case, ALL strings COULD have the same 16 first characters: "SELECT * FROM someTable WHERE ...". Just a warning, if you actually ARE still using 1.2. See http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf ), and your HashMaps load factor, your HashMap could possible degenerate into a linked list, making the amout of .equals operations that must be performed worst case O(n) on the number of Strings in the map.

So, you can use any string you would like, but you will have to pay efficiency penalties for longer strings, and possibly for poor management of your HashMap.

Also, if you are not sure about the max lenght of Strings (theoretically, there is no max, but practically, there is), there is an interesting discussion on it here .

- Adam
 
Jay Damon
Ranch Hand
Posts: 282
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest and Adam for responding to my question. I didn't think there was any special limit so thanks for confirming that for me.
 
pascal betz
Ranch Hand
Posts: 547
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.length() returns an int, so its as Ernest said... and this is farfarfar away from 1000 characters :-)



pascal
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey


Long back, once somebody asked similar question that how long array we can declare in Java and Jim (of course I meant Jim Yingst) answered it...which made sense and I confirmed it too

So assuming we have all the memory available to allocate a single String we can maximum go upto Integer.MAX_VALUE as length() method returns "int" and similar thing applies to array's length too..

So in actual case it won't be exactly Integer.MAX_VALUE as we always have something loaded in memory (like class objects, other possible constants)...

Regards
Maulin
 
Courtney Smythe
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if there are no limits in the String class, you of course are limited by memory.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic