-
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?
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks