What are the storage where Java program can store data


Every computer programs stores data while running and Java also stores data in memory. There are six places where java can store data while running.

Registers:

This is fastest storage where program can store data because registers storage exist inside processor. But register storage is very limited so registers are allocated by the compiler according to its needs. In Java you will not found anything to specify register storage virtually it doesn\’t exists.

Stack:

This lives in the general random-access memory (RAM) area, but has direct support from the processor via its stack pointer. The stack pointer is moved down to create new memory and moved up to release that memory. This is an extremely fast and efficient way to allocate storage, second only to registers.
The Java compiler must know, while it is creating the program, the exact size and lifetime of all the data that is stored on the stack, because it must generate the code to move the stack pointer up and down. This constraint places limits on the flexibility of your programs, so while some Java storage exists on the stack— in particular, object references—Java objects themselves are not placed on the stack.

Heap:

This is a general-purpose pool of memory in the RAM area where all Java objects live. The nice thing about the heap is that, unlike the stack, the compiler doesn’t need to know how much storage it needs to allocate from the heap or how long that storage must stay on the heap. Thus, there’s a great deal of flexibility in using storage on the heap. Whenever you need to create an object, you simply write the code to create it by using new, and the storage is allocated on the heap when that code is executed. Of course there’s a price you pay for this flexibility. It takes more time to allocate heap storage than it does to allocate stack storage

Static:

“Static” is used here in the sense of “in a fixed location” (although it’s also in RAM). Static storage contains data that is available for the entire time a program is running.

You can use the static keyword to specify that a particular element of an object is static, but Java objects themselves are never placed in static storage.

Constant:

Constant values are often placed directly in the program code, which is safe since they can never change.

Sometimes constants are cordoned off by themselves so that they can be optionally placed in read-only memory (ROM), in embedded systems.

Non-RAM storage: If data lives completely outside a program, it can exist while the program is not running, outside the control of the program. Data can be stored on hard disk, memory cards or transferred over network using different stream and serialization. In this type of storage data exist even program is not running