String的Java中的最大长度 - 调用length()方法 [英] String's Maximum length in Java - calling length() method
问题描述
在 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 []
;
推荐答案
考虑 String
class' length
方法返回 int
,即返回的最大长度该方法将是 Integer .MAX_VALUE
, 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)实现的,第10章:数组 Java语言规范,Java SE 7版说明如下:
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:
数组中包含的变量
没有名字;相反,它们是由数组访问表达式
引用的
,它们使用非负整数索引
值。这些变量称为数组的
组件。如果数组
有n
组件,我们说n
是
长度的数组;
的组件使用整数
索引从0
引用n - 1
,包含。
The variables contained in an array have no names; instead they are referenced by array access expressions that use nonnegative integer index values. These variables are called the components of the array. If an array has
n
components, we sayn
is the length of the array; the components of the array are referenced using integer indices from0
ton - 1
, inclusive.
此外,索引必须是 int
值,如在第10.4节中提到:
Furthermore, the indexing must be by int
values, as mentioned in Section 10.4:
数组必须由
int
值编制索引;
因此,似乎限制确实是 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()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!