问题描述
在 Java 中, String
对象的最大大小是多少,指的是 length()
方法调用?
In Java, what is the maximum size a String
object may have, referring to the length()
method call?
我知道 length()
返回<$的大小c $ c>字符串作为 char []
;
推荐答案
考虑 class'方法返回 int
,即返回的最大长度该方法将是, 2 ^ 31 - 1
(或约20亿美元)。
Considering the String
class' length
method returns an int
, the maximum length that would be returned by the method would be Integer.MAX_VALUE
, which is 2^31 - 1
(or approximately 2 billion.)
在数组的长度和索引方面,(例如 char []
,这可能就是内部数据表示是为 String
s)实现的, 说明如下:
In terms of lengths and indexing of arrays, (such as char[]
, which is probably the way the internal data representation is implemented for String
s), Chapter 10: Arrays of The Java Language Specification, Java SE 7 Edition says the following:
此外,索引必须是 int
值,如在中提到:
Furthermore, the indexing must be by int
values, as mentioned in Section 10.4:
因此,似乎限制确实是 2 ^ 31 - 1
,因为这是最大值对于非负 int
值。
Therefore, it appears that the limit is indeed 2^31 - 1
, as that is the maximum value for a nonnegative int
value.
但是,可能会有其他限制,例如最大可分配数组的大小。
However, there probably are going to be other limitations, such as the maximum allocatable size for an array.
这篇关于String的Java中的最大长度 - 调用length()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!