+ 1

What is the difference between length() and capacity() methods in StringBuilder ?

I can't understand the difference between them , I mean When should i use length() and when should i use capacity() in StringBuilder?

7th May 2022, 7:52 PM
Mohamed
Mohamed - avatar
2 Answers
+ 1
length() returns the length or number of characters currently.. capacity() returns the maximum characters or amount storage available for current object that it can hold.. Defaultly it is 16 on empty StringBuilder.. It will be doubled on exceed in length each time. Use length() to get length of string...
7th May 2022, 8:01 PM
Jayakrishna šŸ‡®šŸ‡³
+ 2
capacity is allocated length is used
7th May 2022, 9:25 PM
zemiak