简体   繁体   English

可以String保持大于Integer.MAX_VALUE个字符数

[英]Can String hold greater than Integer.MAX_VALUE number of character

If I want to create a String object which contains x number of character, where x > Integer.MAX_VALUE, can I do that? 如果我想创建一个包含x个字符的String对象,其中x> Integer.MAX_VALUE,我可以这样做吗?

Thanks. 谢谢。

Have a look at the source . 看看来源

the field count , which indicates the string's size is an int - so you will get an overflow. 字段count ,表示字符串的大小为int - 因此您将获得溢出。

private final int count;

Instead of storing a single String of length 2 bn (This will use 8 GB of memory to create btw) You can create a collection of Strings. 而不是存储长度为2 bn的单个字符串(这将使用8 GB的内存来创建btw)您可以创建字符串集合。 Its not as easy to work with but can effectively be any length. 它不是那么容易使用,但可以有效地使用任何长度。

Since String in java is a reference type, strings are stored in a contiguous block of memory. 由于java中的String是引用类型,因此字符串存储在连续的内存块中。 this block must be accessible by integer indices. 必须可以通过整数索引访问此块。 The memory range must be between 0 and 2^32 -1 in a 32 bits computer architecture which is equal to int primitive data type range. 在32位计算机体系结构中,存储器范围必须在0和2 ^ 32 -1之间,该体系结构等于int原始数据类型范围。

A basic integer data type can address your memory range. 基本整数数据类型可以解决您的内存范围。 Therefore, you can not store any string which exceeds your memory. 因此,您不能存储超出内存的任何字符串。

In addition, you can not store any data which exceeds your program stack which is very limited range of memory compared to system memory. 此外,您不能存储超出程序堆栈的任何数据,与系统内存相比,这是非常有限的内存范围。 you will get stackOverFlow exception when the application memory is exceeded. 超出应用程序内存时,您将获得stackOverFlow异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果String包含的数字大于Integer.MAX_VALUE - If a String containing a number bigger than Integer.MAX_VALUE 如何知道float值是否大于Integer.MAX_VALUE? - How to know if the float value is greater than Integer.MAX_VALUE? 如果元素总数大于Integer.MAX_VALUE,我们可以使用IntStream#sum吗? - Can we use IntStream#sum, If sum of elements is greater than the Integer.MAX_VALUE? 解析整数字符串(大于Integer.MAX_VALUE) - Parse integer string (larger than Integer.MAX_VALUE) 如果实际大小大于 Integer.MAX_VALUE,如何找出 java.util.List 的大小? - How can I find out the size of a java.util.List if the actual size is greater than the Integer.MAX_VALUE? 如果列表中的元素数量大于 Integer.MAX_VALUE,LinkedList 是否违反合同? - Is LinkedList breaks contract if amount of elements in list greater than Integer.MAX_VALUE? 选择大于Integer.MAX_VALUE的范围内的随机整数? - Choose random integer in a range bigger than Integer.MAX_VALUE? Java 数据结构,用于任意大量元素且大于 Integer.MAX_VALUE - Java data structure for an arbitrary large number of elements and larger than Integer.MAX_VALUE 如何在jvm中创建多个Integer.MAX_VALUE对象? - How to create more than Integer.MAX_VALUE objects in a jvm? 如果我组件的preferredSize比Integer.MAX_VALUE高怎么办? - What if the preferredSize of my component is taller than Integer.MAX_VALUE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM