- Java 9 Programming By Example
- Peter Verhas
- 460字
- 2021-07-02 23:37:33
Arrays
Variables can be a primitive type according to their declaration, or they may hold a reference to an object. A special object type is an array. When a variable holds a reference to an array, then it can be indexed with the [ and ] characters, along with an integral value consisting of 0 or a positive value ranging to one less than the array's length, to access a certain element of the array. Multi-dimensional arrays are also supported by Java when an array has elements that are also arrays. Arrays are indexed from zero in Java. Under or over indexing is checked at runtime, and the result is an exception.
An exception is special condition that interrupts the normal execution flow and stops the execution of the code or jumps to the closest enclosing catch statement. We will discuss exceptions and how to handle them in the next chapter.
When a code has an array of a primitive type, the array contains many memory slots, each holding the value of the type. When the array has a reference type, in other words, when it is an array of objects, then the array elements are references to objects, each containing the type. In the case of int for example, each element of the array is 32-bit, which is 4 bytes. If the array is a type of Integer, then the elements are references to objects, pointers, so to say, which is usually 64-bit using 64-bit JVM and 32-bit on 32-bit JVM. In addition to that, there is an Integer object somewhere in memory that contains the 4-byte value and also an object header that may be as much as 24 bytes.
The actual size of the extra information needed to administer each object is not defined in the standard. It may be different on different implementations of the JVM. The actual coding, or even the optimization of the code in an environment, should not depend on the actual size. However, the developers should be aware that this overhead exists and is in the range of around 20 or so bytes for every object. Objects are expensive in terms of memory consumption.
Memory consumption is one issue, but there is something else. When the program works with a large amount of data and the work needs the consecutive elements of the array, then the CPU loads a chunk of memory into the processor cache. It means that the CPU can access elements of the array that are consecutively faster. If the array is of a primitive type, it is fast. If the array is of some class type, then the CPU has to access memory to get the actual value, which may be as much as 50 times slower.
- 深度實踐OpenStack:基于Python的OpenStack組件開發(fā)
- AngularJS入門與進階
- DevOps Automation Cookbook
- Learn Swift by Building Applications
- TypeScript圖形渲染實戰(zhàn):基于WebGL的3D架構與實現(xiàn)
- Interactive Applications Using Matplotlib
- 小程序開發(fā)原理與實戰(zhàn)
- Hands-On Functional Programming with TypeScript
- 學習正則表達式
- HTML5+CSS3 Web前端開發(fā)技術(第2版)
- Python機器學習:預測分析核心算法
- Programming with CodeIgniterMVC
- QGIS Python Programming Cookbook(Second Edition)
- Python:Deeper Insights into Machine Learning
- Web Developer's Reference Guide