Java StringBuilder 可以容纳多少个字符?
How many characters can a Java StringBuilder hold?
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);
}
StringBuilder 变量 'url' 是否有最大容量,或者它可以容纳所有内容?
是的,它对最大整数的容量有限制,即 2147483647(技术上)。
StringBuilder
内部保存 char[]
object, and array has limitation in size.
中的字符
如果你完成这个 link 这可能会让你更清楚 Oracle Docs String Builder Buffer Capacity
现在您要声明任何 StringBuilder Class 的容量,然后为此定义一个构造函数 StringBuilder(int initCapacity)
。
StringBuilder(int initCapacity)
:- Creates an empty string builder with the specified initial capacity.
这里因为参数为int
所以StringBuilder
Class可以达到的最大容量是2147483647
.
在StringBuilder
Class中有多种关于Capacity上下文的方法,这些方法也考虑了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.
这些方法也采用 int
类型的参数。因此,使用这些方法或构造函数,您将能够生成最大容量为 2147483647
.
的对象
Java9介绍JEP 254: Compact Strings。这允许通过将字符存储在字节数组中来更 space 高效地存储字符串。如果所有字符都是 Latin 1,则每个字符使用一个字节,否则每个字符使用两个字节。
所以答案是:如果启用紧凑字符串(默认)并且您的 StringBuilder
仅包含 Latin 1 字符,则最大大小为 Integer.MAX_VALUE
*,否则为Integer.MAX_VALUE / 2
*.
*或略少,见"Do Java arrays have a maximum size?"
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);
}
StringBuilder 变量 'url' 是否有最大容量,或者它可以容纳所有内容?
是的,它对最大整数的容量有限制,即 2147483647(技术上)。
StringBuilder
内部保存 char[]
object, and array has limitation in size.
如果你完成这个 link 这可能会让你更清楚 Oracle Docs String Builder Buffer Capacity
现在您要声明任何 StringBuilder Class 的容量,然后为此定义一个构造函数 StringBuilder(int initCapacity)
。
StringBuilder(int initCapacity)
:- Creates an empty string builder with the specified initial capacity.
这里因为参数为int
所以StringBuilder
Class可以达到的最大容量是2147483647
.
在StringBuilder
Class中有多种关于Capacity上下文的方法,这些方法也考虑了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.
这些方法也采用 int
类型的参数。因此,使用这些方法或构造函数,您将能够生成最大容量为 2147483647
.
Java9介绍JEP 254: Compact Strings。这允许通过将字符存储在字节数组中来更 space 高效地存储字符串。如果所有字符都是 Latin 1,则每个字符使用一个字节,否则每个字符使用两个字节。
所以答案是:如果启用紧凑字符串(默认)并且您的 StringBuilder
仅包含 Latin 1 字符,则最大大小为 Integer.MAX_VALUE
*,否则为Integer.MAX_VALUE / 2
*.
*或略少,见"Do Java arrays have a maximum size?"