• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

number of bytes allocated for each entry in arraylist

 
Manish Agar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For an ArrayList, how does java compute the memory location of the object at a particular position in arraylist.

For example:



In above case, we can put any type of object in the ArrayList which might have different memory allocated based on the type of object. When I am trying to get an element, how is the actual memory location is obtained? Does it keep track of all the memory allocated for different types of objects or is there a standard memory size allocated for each object.

Hope I am clear.

Thanks
Manish
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

This has been discussed before but I couldn't find the earlier thread, so I'll repeat the answer.

In Java, you don't store objects - you store references to objects. Each reference has the same fixed size, although the size depends on the JVM implementation. Usually it's either 32 or 64 bits, although that's irrelevant.
The actual objects definitely can have different sizes, but they aren't stored in the array list, just references to them. They themselves are stored on the heap.
 
Manish Agar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Resolved now.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic