First, storage area:
1) register. This is the fastest storage area because it is located in a different location from the other store-the inside of the processor. However, the number of registers is extremely limited, so the registers are allocated according to the requirements. You cannot directly control or feel any sign of register presence in the program.
2) stack. is located in Universal RAM (random access memory), but can receive direct support from the processor through the stack pointer. If the push-stack pointer moves down, the new memory is allocated, and if you move up, the memory is freed. This is a fast and efficient method of allocating storage, second only to registers. When you create a program, the Java system must know the exact life cycle of all items stored in the stack to move the stack pointer up or down. This constraint limits the flexibility of the program, so while some Java data is stored on the stack-especially object references-Java objects are not stored in them.
3) heap. A common memory pool (also located in the Ram area) for storing all Java objects. The benefit of a heap that differs from the stack is that the compiler does not need to know how long the stored data will survive in the heap. Because there is a lot of flexibility in allocating storage in the heap. When you need an object, you only need new to write a simple line of code, and when you execute this line of code, the storage allocation is automatically made in the heap. Of course, there must be a cost to this flexibility: storage allocation and cleanup with heaps can take more time than storage allocation with stacks.
4) constant storage. Constant values are usually stored directly inside the program code, and it is safe to do so because they are never changed. Sometimes, in an embedded system, the constants themselves are isolated from the rest, so in this case, you can choose to store them in ROM (read-only memory).
5) non-RAM storage. If the data is completely out of the program, it can be free from any control of the program and can exist when the program is not running. Two of the basic examples are stream objects and persisted objects. In a Stream object, the object is converted into a byte stream, which is usually sent to another machine. In persisted objects, objects are stored on disk, so they can remain in their own state even if the program terminates. The trick is to turn objects into things that can be stored in other mediums, and revert to regular, ram-based objects when needed. Java provides support for lightweight persistence, and mechanisms such as JDBC and hibernate provide more complex support for storing and reading object information in a database.
Second, special case: Basic type
A series of types are often used in programming, and they require special treatment. You can think of them as "basic" types. The special treatment is because new stores objects in the heap, so creating an object with new-especially small, simple variables-is often not very effective. Therefore, for these types, Java takes the same approach as C and C + +. That is, instead of creating a variable with new, you create an "automatic" variable that is not a reference. This variable stores "values" directly and is placed on the stack, making it more efficient.
Java to determine the amount of storage space that each base type occupies. Their size does not change with the hardware architecture of the machine, as most other languages do. This immutability of the amount of storage space is one of the reasons why Java programs are more portable than programs written in most other languages.
Third, arrays in Java
When you create an array object, you actually create a reference array, and each reference is automatically initialized to a specific value that has its own keyword, NULL, and once Java sees NULL, it knows that the reference is not pointing to an object. You must specify an object for any object before you can use it, and if you try to use a or null reference, you will get an error at run time.
Java Storage Objects