Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

java - Max size for StringBuffer

Why would the StringBuffer have a limit on its size?

I went through some of the links : http://www.coderanch.com/t/540346/java/java/maximum-size-hold-String-buffer.

Is that because of the count member variable, which is an int?

Suppose that we have 2^31-1 chars in StringBuffer and that we append some more chars to that StringBuffer. Count member variable would be incremented by the number of chars appended and if Count variable is already at its max (2^31-1), it would revert back to some negative value.

Why would it throw an error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

because stringbuffer internally uses an array and the maximum number of elements an array can accommodate is 2^31-1 if you increment after reaching this it will go to negative and throws the error


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...