What is the maximum character limit that String
and StringBuffer
types support in Java?
What is the maximum character limit that String
and StringBuffer
types support in Java?
The theoretical limit is the maximum value of a int
- 2^31-1
- since the strings are represented internally by an array of char
, and the way to index this array is int
. However, what will limit in practice the size of the strings is the physical space (RAM), which will certainly be much smaller than this theoretical limit.
I do not know how StringBuffer
is represented internally, but even though it has representation capable of supporting more than the mentioned limit, how to refer to a particular character ( charAt
) still uses a int
, so you can consider the same limit in practice.
The maximum limit they both support is 2 ^ 31- 1 (or approximately 2 billion)
StringBuffer gives you efficiency in string concatenation