• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there anyway to find out the maximum length allowed for java.lang.String?  RSS feed

 
Gurumurthy Ramamurthy
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read somewhere that Integer.MAX_VALUE (2^31-1) is the way.
Is it true?

Thanks,
Guru
 
Tim Moores
Saloon Keeper
Posts: 4032
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be 2GB, so you'd have to run the JVM with more than that amount of memory, or you'd get an OutOfMemoryError before reaching that limit.
The source code of the java.lang.String class -to be found in a file called src.zip somewhere in your JDK installation- should tell you for sure.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it really so interesting to know what the limit is to the amount of data that you can store in a String? If you are writing a program which migh approach this limit, then you are probably making a mistake in the design.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't say in the String code, but you can see that there is a char[] hidden in the String class. Now the index of an array is an int, so the largest you can declare is 2147483647, which is equal to 0x7fffffff or 2 to the 31st - 1, as you have already been told.
If you look in the Java Language Specification, you can see exactly what it says about maximum sizes of arrays. It tells you . . . nothing.
 
Manjunatha Kampli Kottal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jesper de Jong . String are not meant to be hold such a large data. String are mainly designed to hold temporary data. you should relook into your design.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!