Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

When Would You Use capacity()?

 
Ranch Hand
Posts: 87
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm watching and learning about StringBuffer() and StringBuilder() and there are a lot of methods to use on Strings many of which I'm not seeing an application as to when you would have a need to use some of them.

For example, when/why would you need to use the capacity() Method in a Production Environment?

Thenks.
 
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about StringBuffer. You are unlikely to need synchronisation.
What does that method do? Can you think of any circumstances where the length of the text required is much larger than the capacity? Or where the capacity is much larger than any likely text length?
 
Royale Summers
Ranch Hand
Posts: 87
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell -

Great question but it appears that you answered a question with a question...I don't know why I would need to check the capacity(0 if it grows to meet the need of what's being placed in it.
 
Campbell Ritchie
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the default capacity? What would happen if you want to edit some text to be 1,000,000 characters long? That is something in the region of 250 pages. What if your capacity is 1,000,000 and you want a ten‑letter word?
 
Ranch Foreman
Posts: 240
5
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the capacity of a StringBuilder shouldn't need to be changed, if it represents a single line in a text file and the line is of a reasonable size. you can use a String[] to hold multiple lines.

the thing that might matter is the buffer size of an input/output stream for a file reader/writer. if you have an extremely large text file, tens of thousands of lines long, for example. i'm not sure what the most optimal way to process that would be.
 
Campbell Ritchie
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

S Fox wrote:. . . . if you have an extremely large text file, tens of thousands of lines long, for example. i'm not sure what the most optimal way to process that would be.

Would you want to copy the entire file into a String? Wouldn't you create an object to encapsulate the contents of each line instead.
 
Royale Summers
Ranch Hand
Posts: 87
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still not seeing how you would use the capacity method.  If you have an extremely large file then you should know that you more than likely exceeded capacity and would need a program solution to resolve.

Maybe, you can use it as part of the development process when testing?  That's all that I can guess.  I'll keep searching for an answer, thanks....
 
S Fox
Ranch Foreman
Posts: 240
5
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm working on a project of my own right now, it reads in an incredibly large text file, just reads in one line at a time, modifies the line to remove unwanted chars and converts it to upper, and then writes the modified line to a new file. the reading/writing of the large file takes a good 2 minutes long.

on my input stream i can choose to read in more than one line at a time to a buffer, i'm not sure if this would actually improve performance or not.
when i first started working with files, i did just read everything from a file to a single string, but those were very small files.

if i tried to hold the entire contents of this file into memory it would probably crash the jvm, for running out of heap space.
i have crashed the jvm before like that by writing too many millions of entries into a hashset.
 
Campbell Ritchie
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Royale Summers wrote:Still not seeing how you would use the capacity method. . . .

If you know how many Strings you are going to concatenate with a StringBuilder, and you can guess their average length, you will get better performance like this:-Obviously that will only have an effect if your count is large, and you are only going to use it rarely.
 
Royale Summers
Ranch Hand
Posts: 87
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell -

Thanks for the example. This clarifies a lot!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The capacity() is used when using stringBuilder
eg stringBuilder myString = new stringBuilder("red")
 
Saloon Keeper
Posts: 2898
370
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuilder has a number of different constructors.  

In your example, it is using the StringBuilder(String str) constructor, which "Constructs a string builder initialized to the contents of the specified string. The initial capacity of the string builder is 16 plus the length of the string argument."

This is different than the StringBuilder(int capacity) constructor which "Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument."
 
Ron McLeod
Saloon Keeper
Posts: 2898
370
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asamaning Redolf - Welcome to the Ranch
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!