This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Java in General and the fly likes number of bytes allocated for each entry in arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "number of bytes allocated for each entry in arraylist" Watch "number of bytes allocated for each entry in arraylist" New topic
Author

number of bytes allocated for each entry in arraylist

Manish Agar
Greenhorn

Joined: Sep 02, 2011
Posts: 2
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

Joined: Oct 27, 2005
Posts: 20048
    
  30

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Manish Agar
Greenhorn

Joined: Sep 02, 2011
Posts: 2
Thanks. Resolved now.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20048
    
  30

You're welcome.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: number of bytes allocated for each entry in arraylist
 
It's not a secret anymore!