0
short[] foo = new short[45];

assuming that a short occupies 2 bytes and that the array starts at address 5342, which locations does foo[24] occupy? If possible please show how you figure it out and individually list ALL the addresses occupied.

This is not homework, i'm asking this because i really don't understand how to do this and it would be a great if i can see the solution so i can study

also, is this how i should approach this question, 5342+2*24

JavaBeginner
  • 105
  • 6
  • 2
    Can you explain what you would hope to achieve with that information? Bear in mind that arrays are objects - so there's also the type information, a monitor for synchronization, and the length... – Jon Skeet Mar 09 '15 at 08:23
  • possible duplicate of [How does Java store primitive types in RAM?](http://stackoverflow.com/questions/20832717/how-does-java-store-primitive-types-in-ram) – Eel Lee Mar 09 '15 at 08:23

2 Answers2

0

Try this to get the reference address: Is there a way to get a reference address?

Your approach of 5342+2*24 does't sound promising to me as Java array is sequential in virtual memory not necessarily in physical memory Java Array Memory Location

Community
  • 1
  • 1
Abhijeet Shukla
  • 143
  • 3
  • 9
0

after working with a friend i was able to figure out how what to do.

the starting address: 5342 short occupied 2 bytes

==> starting address = b+i*s

b, base address of array: 5342

i, address of element: 24

s, size in bytes: 2

==>starting address: 5342 + (24)*2 = 5390

location foo[24] occupied (ALL address occupied) 5390 (1 byte) to 5391 (1 byte) ==> 2 bytes

JavaBeginner
  • 105
  • 6