中字符串的最大长度

中字符串的最大长度

本文介绍了Java 中字符串的最大长度 - 调用 length() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 中,String 对象的最大大小是多少,参考 length() 方法调用?

In Java, what is the maximum size a String object may have, referring to the length() method call?

我知道 length()String 的大小作为 char [] 返回;

I know that length() return the size of a String as a char [];

推荐答案

考虑String 类'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[],这可能是Strings内部数据表示的实现方式),第 10 章:http://docs.oracle.com/javase/specs/jls/se7/html/index.html" rel="noreferrer">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 Strings), Chapter 10: Arrays of The Java Language Specification, Java SE 7 Edition says the following:

数组中包含的变量没有名字;相反,他们是由数组访问表达式引用使用非负整数索引值.这些变量被称为数组的组件.如果一个数组有 n 个组件,我们说 n 是数组的长度;的组成部分使用整数引用数组从 0n - 1 的索引,包括在内.

此外,索引必须通过 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.

这篇关于Java 中字符串的最大长度 - 调用 length() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:05