2016-06-28 19 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

相關問題