一个Java字符串可以有多少个字符?

时间:2021-09-02 00:01:39

I'm trying The Next Palindrome problem 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在线法官(SPOJ)的下一个Palindrome问题,我需要在那里找到一个高达一百万位数的整数的回文。我考虑过使用Java的函数来反转字符串,但是它们允许字符串这么长吗?

8 个解决方案

#1


186  

You should be able to get a String of length Integer.MAX_VALUE (always 2147483647 (231 - 1) by the Java specification, the maximum size of an array, which the String class uses for internal storage) or half your maximum heap size (since each character is two bytes), whichever is smaller.

您应该能够得到一个长度为整数的字符串。MAX_VALUE(总是2147483647(231 - 1),由Java规范、数组的最大大小、字符串类用于内部存储的大小)或最大堆大小的一半(因为每个字符都是两个字节),两者都是较小的。

#2


17  

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 ^还有人物,因为它们是由一个内部数组,并通过Java中整数数组索引。

#3


11  

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

虽然你可以在理论上是整数。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上更新了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中,字符串将使用字节[],这意味着多字节字符将使用多个字节,并进一步减少最大值。如果你有四个字节的代码点,比如emojis,你只会得到大约5亿个字符。

#4


5  

Have you considered using BigDecimal instead of String to hold your numbers?

你有没有考虑过使用BigDecimal而不是字符串来保存数字?

#5


3  

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

整数。MAX_VALUE是字符串的最大大小,取决于您的内存大小,但是在sphere的在线判断上,您不需要使用这些函数。

#6


2  

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[]来存储字符串。值,所以您只能在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.

我的意思是“char”,有些字符在BMP中是不能被表示的(就像一些表情符号一样),所以它需要更多的字符(目前是2)。

#7


0  

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位。

#8


-3  

If you use google's app engine, com.google.appengine.api.datastore.Text can help. It allows a single string to store upto1 megabyte.

如果你使用谷歌的appengine, com.google.appengine.api.datastore。文本可以帮助。它允许单个字符串存储upto1兆字节。

#1


186  

You should be able to get a String of length Integer.MAX_VALUE (always 2147483647 (231 - 1) by the Java specification, the maximum size of an array, which the String class uses for internal storage) or half your maximum heap size (since each character is two bytes), whichever is smaller.

您应该能够得到一个长度为整数的字符串。MAX_VALUE(总是2147483647(231 - 1),由Java规范、数组的最大大小、字符串类用于内部存储的大小)或最大堆大小的一半(因为每个字符都是两个字节),两者都是较小的。

#2


17  

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 ^还有人物,因为它们是由一个内部数组,并通过Java中整数数组索引。

#3


11  

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

虽然你可以在理论上是整数。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上更新了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中,字符串将使用字节[],这意味着多字节字符将使用多个字节,并进一步减少最大值。如果你有四个字节的代码点,比如emojis,你只会得到大约5亿个字符。

#4


5  

Have you considered using BigDecimal instead of String to hold your numbers?

你有没有考虑过使用BigDecimal而不是字符串来保存数字?

#5


3  

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

整数。MAX_VALUE是字符串的最大大小,取决于您的内存大小,但是在sphere的在线判断上,您不需要使用这些函数。

#6


2  

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[]来存储字符串。值,所以您只能在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.

我的意思是“char”,有些字符在BMP中是不能被表示的(就像一些表情符号一样),所以它需要更多的字符(目前是2)。

#7


0  

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位。

#8


-3  

If you use google's app engine, com.google.appengine.api.datastore.Text can help. It allows a single string to store upto1 megabyte.

如果你使用谷歌的appengine, com.google.appengine.api.datastore。文本可以帮助。它允许单个字符串存储upto1兆字节。