array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: array

  1. #1
    sophia Guest

    array


    int[] primes = new int[10];
    I have read a java book, beginning java 2(Ivor Horton) page 111, it says
    each element in the primes array is an int variable requiring 4 bytes, so
    the whole array will occupy 40 bytes, and plus 4 bytes to store the reference
    to the array. However, I couldn't understand why it needs plus another 4
    bytes to store the reference to the array?



  2. #2
    Kyle Gabhart Guest

    Re: array


    "sophia" <kuan_yun@hotmail.com> wrote:
    >
    >int[] primes = new int[10];
    > I have read a java book, beginning java 2(Ivor Horton) page 111, it says
    >each element in the primes array is an int variable requiring 4 bytes, so
    >the whole array will occupy 40 bytes, and plus 4 bytes to store the reference
    >to the array. However, I couldn't understand why it needs plus another

    4
    >bytes to store the reference to the array?
    >
    >


    Sophia,

    Good question. An array is an object (you'll discover these in the next
    chapter, Chapter 5). As an object, java allocates a location in memory to
    store the contents of the array. Objects are powerful structures that allow
    you to invoke methods upon them, operate on their state variables, and even
    combine them to define a larger object. You can have multiple variables
    reference an object and pass this reference to methods, classes, and even
    other Java Virtual Machines.

    All of this flexibility that objects provide does not come without a price.
    That price, in the case of an array, costs 4 bytes to store a reference
    to the array in memory. Additional references would also cost 4 bytes of
    memory.

    I hope this explanation is helpful. Feel free to post another question or
    e-mail me.

    Happy Coding!

    Cordially,

    Kyle Gabhart
    DevX Java Pro


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center