StringBuffer的最大大小 [英] Max size for StringBuffer

查看:235
本文介绍了StringBuffer的最大大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 StringBuffer 的大小有限制?

我浏览了一些链接:
http://www.coderanch .com / t / 540346 / java / java / maximum-size-hold-String-buffer

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

这是因为count成员变量,是一个int?

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

假设我们在 StringBuffer 中有2 ^ 31-1个字符,我们追加了一些chars到 StringBuffer 。计数成员变量将增加附加的字符数,如果计数变量已经达到其最大值(2 ^ 31-1),它将恢复为某个负值。

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.

为什么会抛出错误?

推荐答案

因为stringbuffer内部使用数组并且数组可以包含最大元素数如果在达到此值后递增,则适应为2 ^ 31-1,它将转为负值并抛出错误

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

这篇关于StringBuffer的最大大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆