Java String 可以有多少个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1179983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:34:45  来源:igfitidea点击:

How many characters can a Java String have?

javastring

提问by andandandand

I'm trying The Next Palindromeproblem from Sphere Online Judge (SPOJ) where I need to find a palindrome for a integer of up to a million digits. I thought about using Java's functions for reversing Strings, but would they allow for a String to be this long?

我正在尝试来自 Sphere Online Judge (SPOJ)的下一个回文问题,我需要在其中找到最多一百万位整数的回文。我想过使用 Java 的函数来反转字符串,但是它们会允许字符串这么长吗?

采纳答案by Bill the Lizard

You should be able to get a String of length

你应该能够得到一个长度的字符串

  1. Integer.MAX_VALUEalways 2,147,483,647(231- 1)
    (Defined by the Java specification, the maximum size of an array, which the String class uses for internal storage)
    OR

  2. Half your maximum heap size(since each character is two bytes) whichever is smaller.

  1. Integer.MAX_VALUE始终为2,147,483,647(2 31- 1)
    (由 Java 规范定义,数组的最大大小,String 类用于内部存储)

  2. Half your maximum heap size(因为每个字符是两个字节)以较小者为准

回答by aperkins

I believe they can be up to 2^31-1 characters, as they are held by an internal array, and arrays are indexed by integers in Java.

我相信它们最多可以有 2^31-1 个字符,因为它们由内部数组保存,并且数组在 Java 中由整数索引。

回答by Mite Mitreski

Integer.MAX_VALUE is max size of string + depends of your memory size but the Problem on sphere's online judge you don't have to use those functions

Integer.MAX_VALUE 是字符串的最大大小 + 取决于您的内存大小,但 Sphere 在线判断问题您不必使用这些函数

回答by Thorbj?rn Ravn Andersen

Have you considered using BigDecimalinstead of Stringto hold your numbers?

您是否考虑过使用BigDecimal而不是String保存您的号码?

回答by Joe Plante

The heap part gets worse, my friends. UTF-16 isn't guaranteed to be limited to 16 bits and can expand to 32

堆部分变得更糟,我的朋友们。UTF-16 不保证限于 16 位,可以扩展到 32

回答by Peter Lawrey

While you can in theory Integer.MAX_VALUE characters, the JVM is limited in the size of the array it can use.

虽然理论上您可以使用 Integer.MAX_VALUE 字符,但 JVM 可以使用的数组大小受到限制。

public static void main(String... args) {
    for (int i = 0; i < 4; i++) {
        int len = Integer.MAX_VALUE - i;
        try {
            char[] ch = new char[len];
            System.out.println("len: " + len + " OK");
        } catch (Error e) {
            System.out.println("len: " + len + " " + e);
        }
    }
}

on Oracle Java 8 update 92 prints

在 Oracle Java 8 update 92 上打印

len: 2147483647 java.lang.OutOfMemoryError: Requested array size exceeds VM limit
len: 2147483646 java.lang.OutOfMemoryError: Requested array size exceeds VM limit
len: 2147483645 OK
len: 2147483644 OK

Note: in Java 9, Strings will use byte[] which will mean that multi-byte characters will use more than one byte and reduce the maximum further. If you have all four byte code-points e.g. emojis, you will only get around 500 million characters

注意:在 Java 9 中,字符串将使用 byte[] 这意味着多字节字符将使用一个以上的字节并进一步减少最大值。如果您拥有所有四个字节的代码点,例如表情符号,您将只能获得大约 5 亿个字符

回答by Revin

Java9 uses byte[] to store String.value, so you can only get about 1GB Strings in Java9. Java8 on the other hand can have 2GB Strings.

Java9 使用 byte[] 来存储 String.value,所以在 Java9 中你只能得到大约 1GB 的字符串。另一方面,Java8 可以有 2GB 的字符串。

By character I mean "char"s, some character is not representable in BMP(like some of the emojis), so it will take more(currently 2) chars.

通过字符,我的意思是“字符”,某些字符在 BMP 中无法表示(如某些表情符号),因此需要更多(当前为 2 个)字符。