Maximum number of characters in a String and StringBuffer in Java

4

What is the maximum character limit that String and StringBuffer types support in Java?

    
asked by anonymous 02.10.2014 / 18:28

2 answers

4

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.

    
02.10.2014 / 18:36
2

The maximum limit they both support is 2 ^ 31- 1 (or approximately 2 billion)

StringBuffer gives you efficiency in string concatenation

    
02.10.2014 / 18:34