Home > Java > javaTutorial > body text

How to save data in java

coldplay.xixi
Release: 2020-08-18 14:38:30
Original
5759 people have browsed it

How to save data in java: 1. Use registers to save, which is the fastest saving area; 2. Use stacks, which will create new memory; 3. Use static storage, which will wait for calls at any time; 4. Using constant storage is placed directly inside the program code.

How to save data in java

Java method of saving data:

(1) Register.

This is the fastest save area because it is located in a different place than all other save methods: inside the processor. However, the number of registers is very limited, so registers are allocated by the compiler as needed. We have no direct control over this, and it is impossible to find any trace of the register's existence in our program.

(2) Stack.

Resides in a regular RAM (random access memory) area, but has direct support for processing through its "stack pointer". Moving the stack pointer down creates new memory; moving it up releases that memory. This is an extremely fast and efficient way to save data, second only to registers. When creating a program, the Java compiler must know exactly the "length" and "how long" of all data held on the stack. This is due to the fact that it has to generate the appropriate code to move the pointer up and down. This limitation undoubtedly affects the flexibility of the program, so although some Java data is saved on the stack - especially object handles, Java objects are not placed in it.

(3) Heap.

A general-purpose memory pool (also in the RAM area) in which Java objects are stored. Unlike the stack, the most attractive thing about the "memory heap" or "Heap" is that the compiler does not have to know how much storage space to allocate from the heap, nor does it need to know how long the stored data will stay in the heap. Therefore, you will get greater flexibility when using the heap to save data. When you are required to create an object, you only need to use the new command to compile the relevant code. When these codes are executed, data will be automatically saved in the heap. Of course, this flexibility comes at a price: it takes longer to allocate storage space on the heap!

(4) Static storage.

The "static" here means "located in a fixed location" (although it is also in RAM). While the program is running, the statically stored data will be waiting to be called at any time. You can use the static keyword to indicate that specific elements of an object are static. But Java objects themselves are never placed in static storage.

(5) Constant storage.

Constant values ​​are usually placed directly inside the program code. This is safe because they never change. Some constants need to be strictly protected, so consider placing them in read-only memory (ROM).

(6) Non-RAM storage.

If the data is completely independent of a program, it can still exist when the program is not running and is outside the control scope of the program. Two of the most important examples are "streaming objects" and "fixed objects". For streaming objects, the object is turned into a stream of bytes, usually sent to another machine. For fixed objects, the object is saved on disk. Even if the program is terminated, they can still maintain their state. A particularly useful trick for these types of data storage is that they can exist in other media. They can even be restored to normal, RAM-based objects if needed. Java 1.1 provides support for Lightweight persistence. Future versions may even provide a more complete solution.

Related learning recommendations: java basic tutorial

The above is the detailed content of How to save data in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template