Login    Forum    Search    FAQ   Blog

Board index » Server Side Development » Java




 Page 1 of 1 [ 6 posts ] 



Author Message
 Post subject: Capacity in the StringBuilder
 Post Posted: Fri Nov 27, 2009 8:27 pm 
Offline

Joined: Sat Feb 21, 2009 3:11 pm
Posts: 22
Code:
StringBuilder as=new StringBuilder();
as.ensureCapacity(10);


in dis above codin i coould not ensure dat capacity.....

Code:
as=new StringBuilder("1234567890123"); and as=new StringBuilder("123"); both are run poperly


Still what da use of ensurin the Capacity.......


Top 
 Post subject: Re: Capacity in the StringBuilder
 Post Posted: Fri Nov 27, 2009 8:28 pm 
Offline

Joined: Sat Feb 21, 2009 11:06 am
Posts: 1794
It simply allocats da memory u really need. It was very useful wen u
have 2 work on the huge strings.


Top 
 Post subject: Re: Capacity in the StringBuilder
 Post Posted: Fri Nov 27, 2009 8:28 pm 
Offline

Joined: Sat Feb 21, 2009 1:08 pm
Posts: 549
Code:
ensureCapacity(int minimumCapacity)


Top 
 Post subject: Re: Capacity in the StringBuilder
 Post Posted: Fri Nov 27, 2009 8:28 pm 
Offline

Joined: Sat Feb 21, 2009 3:13 pm
Posts: 35
Mostly bcoz it won't need t2 reassign each n evry thin when allocatin
more memory, which w as the good for performance.

Literally a StringBuffer would start wid the specific buffer size nd then
double dat size whenever it gets within a set limit of da currently used
capacity.
This typically means dat an entirely new buffer would be generated wid da
new size, all the contents 4m the old buffer copied 2 dat , nd the
old buffer released.
When ur buffer gets large dis is both slow nd then takes up a lot of
memory (say you have a 500K buffer now, it will for a while have 1.5M memory in use when you may need just 501K).


Top 
 Post subject: Re: Capacity in the StringBuilder
 Post Posted: Fri Nov 27, 2009 8:28 pm 
Offline

Joined: Sat Feb 21, 2009 3:11 pm
Posts: 22
could u pls say ne kida example for that .....?


Top 
 Post subject: Re: Capacity in the StringBuilder
 Post Posted: Fri Nov 27, 2009 8:29 pm 
Offline

Joined: Sat Feb 21, 2009 11:06 am
Posts: 1794
Repeating what has already been said really but hoprefully you might understand this way maybe?

Code:
StringBuilder as=new StringBuilder(10);  //Has a capacity of 10
as.ensureCapacity(20);  //Increases Capacity to 20



If it goes over 20 it increases the capacity (eg. ends up at a size of 30) but if you set it to 30 to start with because you know it will be 30 then you are being more efficient, instead of just letting the StringBuilder keep increasing capacity several times because it runs out fo space you set it to an initial capacity of 30 and thus have an efficiently sized stringbuilder and less operations are performed...

Thats all capacity is for, not a limit.

Regards,


Top 
Display posts from previous:  Sort by  
 
 Page 1 of 1 [ 6 posts ] 




Board index » Server Side Development » Java


Who is online

Users browsing this forum: No registered users and 2 guests

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron