2016-06-28 22 views
6

StringBuilder是否具有JAVA中最大容量的字符数限制。Java StringBuilder可以容纳多少个字符?

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'有最大容量,还是它可以容纳所有东西?

回答

7

是的,它最大整数的容量限制在2147483647(技术上)。

StringBuilder内部容纳char[]对象中的字符,并且数组的大小有限制。 read more about it on other thread

+2

如果它超过最大限制会发生什么? –

0

如果你通过这个链接这可能会清除你更Oracle Docs String Builder Buffer Capacity

现在想声明任何StringBuilder类的容量,然后一个构造StringBuilder(int initCapacity)为这个定义。

StringBuilder(int initCapacity) :- Creates an empty string builder with the specified initial capacity.

这里因为参数作为int一个StringBuilder类可以是达到的最大容量将是2147483647

关于容量的上下文有各种方法在StringBuilder类别中,那些方法也考虑类型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的对象。

+4

您所展示的方法都不具备* maximum *容量,这就是问题所在。您提供的链接也没有提及最大容量。至于你的最后一句话,只是因为参数是一个“int”,并不意味着如果你给出了最大可能的整数值,你将不会得到一个错误,而'new StringBuilder(Integer.MAX_VALUE)'将得到' OutOfMemoryError:无论分配给VM多少内存,请求的数组大小都超过VM限制。简而言之,这个答案中的任何一部分都不适用于这个问题。 – Andreas

+0

@Andreas是的你是对的。如果没有足够的内存,则会引发'OutOfMemory' JVM错误。我没有提到这个错误。 谢谢 –

+0

即使有足够的内存*是*可用,并不意味着'新的字符[Integer.MAX_VALUE]'将工作。数组的最大大小可能受其他约束的限制,这些约束与平台有关。 – Andreas

相关问题