Java StringBuilder 可以容纳多少个字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38067717/
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
How many characters can a Java StringBuilder hold?
提问by Narendra Rawat
Does StringBuilder have a limit of characters for maximum capacity in JAVA.
StringBuilder 是否有 JAVA 中最大容量的字符限制。
StringBuilder url=new StringBuilder();
stmt = connnection.createStatement();
String sql="SOME QUERY";
rs = stmt.executeQuery(sql);
while(rs.next())
{
String emailId=rs.getString("USER_EMAIL_ID");
url.append(emailId);
}
does StringBuilder variable 'url' have a maximum capacity, or it can hold everything?
StringBuilder 变量 'url' 是否有最大容量,或者它可以容纳所有内容?
回答by Subhrajyoti Majumder
Yes, it has limitation in capacity of max integer which 2147483647(technically).
是的,它对最大整数的容量有限制,即 2147483647(技术上)。
StringBuilder
internally holds chracters in char[]
object, and array has limitation in size. read more about it on other thread
StringBuilder
内部在char[]
对象中保存字符,数组有大小限制。在其他线程上阅读更多相关信息
回答by Vikrant Kashyap
If you go through with this link this may clear you more Oracle Docs String Builder Buffer Capacity
如果您浏览此链接,这可能会让您了解更多Oracle Docs String Builder Buffer Capacity
Now you want to declare the Capacity of any StringBuilder Class, then one Constructor StringBuilder(int initCapacity)
is defined for this.
现在您要声明任何 StringBuilder 类的容量,然后StringBuilder(int initCapacity)
为此定义一个构造函数。
StringBuilder(int initCapacity)
:- Creates an empty string builder with the specified initial capacity.
StringBuilder(int initCapacity)
:- 创建一个具有指定初始容量的空字符串构建器。
Here because of parameter as int
the maximum capacity that a StringBuilder
Class can is reach will be 2147483647
.
这里因为int
一个StringBuilder
Class 可以达到的最大容量的参数是2147483647
.
there are various method regarding this context of Capacity in StringBuilder
Class, those methods also consider the parameters of type int
.
关于StringBuilder
类中容量的这种上下文有多种方法,这些方法也考虑了 type 的参数int
。
void setLength(int newLength):- Sets the length of the character sequence. If newLength is less than length(), the last characters in the character sequence are truncated. If newLength is greater than length(), null characters are added at the end of the character sequence.
void ensureCapacity(int minCapacity):- Ensures that the capacity is at least equal to the specified minimum.
void setLength(int newLength):- 设置字符序列的长度。如果 newLength 小于 length(),则字符序列中的最后一个字符将被截断。如果 newLength 大于 length(),则在字符序列的末尾添加空字符。
void ensureCapacity(int minCapacity):- 确保容量至少等于指定的最小值。
these methods also takes argument as of int
type . So, Using these methods or contructors you will able to generate a object with max capacity of 2147483647
.
这些方法也将参数作为int
type 。因此,使用这些方法或构造函数,您将能够生成最大容量为2147483647
.
回答by Marcono1234
Java 9 introduced JEP 254: Compact Strings. This allows storing strings more space-efficiently by storing characters in a byte array. If all characters are Latin 1, one byte is used per char, otherwise two bytes per char are used.
Java 9 引入了JEP 254: Compact Strings。这允许通过在字节数组中存储字符来更有效地存储字符串。如果所有字符都是拉丁文 1,则每个字符使用一个字节,否则每个字符使用两个字节。
So the answer is: If compact strings are enabled (the default) and your StringBuilder
contains only Latin 1 chars, the maximum size is Integer.MAX_VALUE
*, otherwise it is Integer.MAX_VALUE / 2
*.
所以答案是:如果启用了压缩字符串(默认值)并且您StringBuilder
只包含拉丁语 1 个字符,则最大大小为Integer.MAX_VALUE
*,否则为Integer.MAX_VALUE / 2
*。
*Or slightly less, see "Do Java arrays have a maximum size?"
*或者稍微少一点,请参阅“Java 数组有最大大小吗?”