Maximum length of a string
I need to store a string of length approx 2 60 000 characters. I get an error saying that it is too large. Searching on Google, I found different answers and each one has a reason. Now, what is the maximum length of a String and what do I do if I wish to store strings larger than that maximum size?
Re: Maximum length of a string
class String implements java.io.Serializable {
private char value[]; // 4 bytes + 12 bytes of array header
private int offset; // 4 bytes
private int count; // 4 bytes
}
The largest number a four-byte signed integer can hold is 2,147,483,647.
So I guess that's the max length of a String.
I HOPE THIS HELPS
Re: Maximum length of a string
Quote:
Originally Posted by
HandgunSally
class String implements java.io.Serializable {
private char value[]; // 4 bytes + 12 bytes of array header
private int offset; // 4 bytes
private int count; // 4 bytes
}
The largest number a four-byte signed integer can hold is 2,147,483,647.
So I guess that's the max length of a String.
I HOPE THIS HELPS
Thanks for your reply but I couldn't understand much of it. This is what I actually want to type in the editor:
Code :
String str="000000000 1"+
"000000001 1"+
"000000002 1"+
"000000010 1"+
"000000011 1"+
"000000012 1"+
// and around 20000 lines in this way
+"222222221 1"+
"2222222222 1";
I am getting a compilation error that the String literal is too large. As per the maximum value stated by you, I shouldn't be getting this error. Is there any other alternative way to store this large String literal?
Re: Maximum length of a string
Hardcoding through string addition is possibly not the best practice anyway, depending upon what your needs are. Store the String in a File, or use a StringBuilder or StringBuffer.