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

Answer from Pavan Kumar on Stack Overflow
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java stringbuffer length
Java StringBuffer length() Method
February 13, 2026 - Note − If the string is an empty string but contains some white-spaces, then the length() method counts the white-spaces and returns it as the length of the string. Following is the syntax of the Java StringBuffer length() method −
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › data › accessors.html
Getting the Length of a String or String Buffer
A string buffer or string builder's length is the number of characters it contains; its capacity is the number of character spaces that have been allocated.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › StringBuffer.html
StringBuffer (Java Platform SE 7 )
At any point in time it contains ... but the length and content of the sequence can be changed through certain method calls. String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved. The principal operations on a StringBuffer are the append ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuffer-setlength-in-java-with-examples
StringBuffer setLength() in Java with Examples - GeeksforGeeks
August 19, 2019 - String length = 12 and contains = WelcomeGeeks After setLength() String = WelcomeGee Example 2: ... // Java program to demonstrate // the setLength() Method. class GFG { public static void main(String[] args) { // create a StringBuffer object // with a String pass as parameter StringBuffer str = new StringBuffer("Tony Stark will die"); // print string System.out.println("String length = " + str.length() + " and contains = \"" + str + "\""); // set length equal to 25 str.setLength(25); // print string System.out.println("After setLength() String = \"" + str.toString() + "\""); } } Output:
🌐
Merit Campus
java.meritcampus.com › core-java-topics › java-stringbuffer-length-and-capacity-methods
Java StringBuffer length() And capacity() Methods
April 5, 2016 - Java StringBuffer length() And capacity() Methods: The length is the character count of the sequence of characters currently represented by StringBuffer. The capacity is the amount of storage available for newly inserted characters
🌐
TutorialsPoint
tutorialspoint.com › get-the-length-of-a-stringbuffer-object-in-java
Get the length of a StringBuffer Object in Java
June 26, 2020 - To find the length of the StringBuffer object, the length() function is used. It is a method of the StringBuffer Class. The method returns the count of characters in the sequence. Declaration- The java.lang.StringBuffer.length() method is de
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-stringbuffer-length-method
Java StringBuffer length() Method - GeeksforGeeks
July 23, 2025 - The length() method in the StringBuffer class is used to retrieve the number of characters currently in a StringBuffer object.
Find elsewhere
🌐
Oracle
docs.oracle.com › javame › config › cldc › ref-impl › cldc1.0 › jsr030 › java › lang › StringBuffer.html
java.lang Class StringBuffer
Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string. The initial capacity of the string buffer is 16 plus the length of the string argument.
🌐
Scientech Easy
scientecheasy.com › home › blog › stringbuffer methods in java
StringBuffer Methods in Java - Scientech Easy
June 2, 2025 - In the preceding program, length of StringBuffer object is 12. Its capacity is 28.
🌐
IBM
ibm.com › docs › en › netezza
IBM Netezza Performance Server
Return the current length of the string stored in the StringBuffer.
🌐
IBM
ibm.com › docs › en › netezza
StringBuffer:length()
Return the current length of the string stored in the StringBuffer.
🌐
Scientech Easy
scientecheasy.com › home › blog › stringbuffer class in java
StringBuffer Class in Java - Scientech Easy
May 29, 2025 - Output: Before adding any character: Length = 0 Default initial capacity = 16 After adding 16 characters: Length = 16 Capacity = 16 After adding 17th character: Length = 17 Capacity = 34 After adding 35th character: Length = 35 Capacity = 70 ...
🌐
IBM
ibm.com › docs › en › netezza
StringBuffer:length() - IBM Documentation
January 8, 2026 - Return the current length of the string stored in the StringBuffer.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › StringBuffer.html
StringBuffer (Java SE 11 & JDK 11 )
January 20, 2026 - If the length of the specified CharSequence is less than or equal to zero, then an empty buffer of capacity 16 is returned. ... Compares two StringBuffer instances lexicographically.
🌐
Scaler
scaler.com › home › topics › java › stringbuffer class in java
StringBuffer Class in Java Class - Scaler Topics
February 11, 2022 - Let's understand the method of StringBuffer in java · The above snippet prints the length of the StringBuffer sequence i.e., 6 is the length of the sequence in the above snippet.
🌐
Medium
medium.com › @kranthimumjampalli › stringbuffer-56b23eb96620
StringBuffer. StringBuffer is thread-safe and… | by Kranthi Munjampalli | Medium
May 5, 2024 - StringBuffer allocates room for 16 additional characters when no specific buffer length is requested, because reallocation is a costly process in terms of time. Also, frequent reallocations can fragment memory.
🌐
InterviewBit
interviewbit.com › problems › stringbuffer
StringBuffer | InterviewBit
* Syntax: StringBuffer s = new StringBuffer("InterviewBit"); Methods in StringBuffer Class: * length( ) and capacity( ): The length of a StringBuffer can be found by the length( ) method, while the total allocated capacity can be found by the ...