1

By default when we create an arrayList without specifying its size , its create an array of 10 elements in the memory but my question is, if we create something like List lst = new ArrayList();

How many bytes will be reserved in the memory for lst?

Java_Jack
  • 577
  • 1
  • 5
  • 8

2 Answers2

1

The default capacity of an ArrayList has nothing to do with generics.

If you don't specify its capacity, it will default to a capacity of 10. An Object array of 10 elements will be created, filled with nulls.

An Object array is just an array of object references.

The memory cost of an object reference depends on the platform your program is running (typically 8 bytes on a 64 bits OS, 4 bytes on a 32 bits OS).

Eric Citaire
  • 4,355
  • 1
  • 29
  • 49
0

Java generics are erased (not reified). Instances constructed as

  • new ArrayList()
  • new ArrayList<Object>()
  • new ArrayList<Integer>()

are identical at runtime.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137