In Java, what is the maximum size a String object may have, referring to the length() method call?
I know that length() return the size of a String as a char [];
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Considering the
Stringclass’lengthmethod returns anint, the maximum length that would be returned by the method would beInteger.MAX_VALUE, which is2^31 - 1(or approximately 2 billion.)In terms of lengths and indexing of arrays, (such as
char[], which is probably the way the internal data representation is implemented forStrings), Chapter 10: Arrays of The Java Language Specification, Java SE 7 Edition says the following:Furthermore, the indexing must be by
intvalues, as mentioned in Section 10.4:Therefore, it appears that the limit is indeed
2^31 - 1, as that is the maximum value for a nonnegativeintvalue.However, there probably are going to be other limitations, such as the maximum allocatable size for an array.