Forums Register Login

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

+Pie Number of slices to send: Send
I read somewhere that Integer.MAX_VALUE (2^31-1) is the way.
Is it true?

Thanks,
Guru
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Yeah, but is it art? What do you think tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 842 times.
Similar Threads
Reading file into byte array
"Top Ten Topics that Everyone Thinks are on the SCJP Exam, but Aren't "- Bert Bates
converting from timestamp to date
Maximum length of Command Line Argument
DB error in servlet while inserting data
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:59:57.