How do you determine StringBuffer and List capacity?

hi all,

I'm curious and would like to post this query that how to determine the StringBuffer and List capacity after read some of the Java Platform performance book. Some of this performance books simply tells number of capacity brieftly without telling what and how does the capacity stand for.

First, the book mentioned StringBuffer(50). Note, my question is that what is this 50 stand for? 50 Characters? or any. Can someone help me to rectify this?

Second, List. ArrayList(0.75). And what is this 0.75(by default) stand for? A heap space? Then how many of them? How many objects that can be stored for not to "exceed 0.75"?

Please help. :)

regards,

Elvis

scjp

[720 byte] By [airqq] at [2007-9-26 1:46:56]
# 1

hello:

The class StringBuffer has three constructors:

The first one is StringBuffer(),in this case,the initial capacity of is 16 characters.The Second one is StringBuffer(int length),the initial capacity of is length characters.The third one is StringBuffer(String str),then the initial capacity is str.length() characters.

when it comes to List. ArrayList(0.75),I havn't seen it.In general,the visible rows of a list is specified by the constructor new List(int visibleRows).

If you still have questions,let me know.

horse-

horseliu at 2007-6-29 2:45:21 > top of Java-index,Core,Core APIs...
# 2
hi horse,Thank you for your reply. :)Yes, StringBuffer( 16(default) + the number of charater(variable) = capacity ) is the capacity of a StringBuffer.Anyway, thanks a lot. I'm still looking for List capacity. :)regards,elvis scjp
airqq at 2007-6-29 2:45:21 > top of Java-index,Core,Core APIs...
# 3

I don't know what you mean by "capacity", but what I mean by "capacity" is "the maximum possible amount that a container can hold". Using that definition, 16 is **not** the capacity of a StringBuffer because it will expand to hold whatever you put into it, up to some ridiculously large amount.

If you have a different definition of "capacity" that you are interested in, perhaps you could say what it is?

DrClap at 2007-6-29 2:45:21 > top of Java-index,Core,Core APIs...
# 4
ok DrClap, what is the capacity of having "Who am I" string that a StringBuffer suppose to defined in the constructor?regards,elvisscjp
airqq at 2007-6-29 2:45:21 > top of Java-index,Core,Core APIs...
# 5
by default, then, the size of your StringBuffer is 16, but if you grow the StringBuffer beyond that original allocation, it will (create another StringBuffer object to?) allow it to have a greater size.is this what you're trying to ask?
ldadams at 2007-6-29 2:45:21 > top of Java-index,Core,Core APIs...
# 6

I think the capacity is the CURRENT size of a container. But it is not equal to the number of elements that container currently hold. Just like, a house can have 10 people within, but problably only 3 people at some time. Also, the capacity is not equal to the maximum size, because container can grows automatically.

For example, a container whose capacity is 50, currently has 30 elements in it. If you add 10 elements more. That is only an addition operation. But if you add 30 elements to it. Then the container first enlarge its capacity according to some arithmetic(a enlarging rate), secondly carry out the addition operation.

Now that the capacity is the size, it should be a number standing for HOW MANY elements.... In the case of StringBuffer, it should be how many chars; in the case of ArrayList, it should be how many Objects. I do not think 0.75 can stand for a capacity. Prabaly, it was used to describe the enlarging rate when containers need to contain more elements than its current capacity. ( From JDK API, you can see the type of capacity is int ).

For containers and alike, the questions "how many I can hold" and "how many I am holding", "Do I can enlarge"? are helpful for understanding how it works.

wangriver at 2007-6-29 2:45:21 > top of Java-index,Core,Core APIs...