I want to figure out sometihng.
is this:
List <String> list = new ArrayList<String>();
list.add("abc");
list.add(null);
equals to this
List <String> list = new ArrayList<String>();
list.add("abc");
in memory usage?
thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The initial capacity of an ArrayList is ten (references). That is to say, the underlying array will be of size ten, even if you’ve only got one entry in your collection. Those references will default to null, and consequently setting the second reference to null will neither affect the internal state of the arraylist (in terms of the underlying array) nor its memory consumption.
If you’d added an eleventh item (set to null), the
ArrayListwould expand its capacity, and consequently you’d consume more memory, but rather because the ArrayList had created extra buckets for yourStringreferences.From the doc linked above: