Home >Java >JavaBase >What is a reference variable in Java?

What is a reference variable in Java?

Guanhui
GuanhuiOriginal
2020-06-06 15:01:025994browse

What is a reference variable in Java?

What is a reference variable in Java?

Java's reference variable is actually a pointer, which points to an object instance in the heap memory. It is equivalent to giving a name to an array or object, and then you can use the stack in the program. A reference variable to access an array or object in the heap.

#What is the heap and what is the stack?

Storage area Storage content Advantages Disadvantages Recycling
Stack Basic type variables and object reference variables The access speed is faster than the heap, second only to Registers and stack data can be shared The size and lifetime of the data stored in the stack must be determined and lack flexibility. The stack mainly stores some basic types of variables When the scope of the variable is exceeded, Java will automatically release the variable, and the memory space can be immediately used for other purposes
Heap Objects and arrays created by instructions such as new can dynamically allocate memory size, and the lifetime does not need to be told to the compiler in advance Because it needs to be run at runtime Dynamically allocates memory, and the access speed is slow The automatic garbage collector of the Java virtual machine recycles data that is no longer used

Storage characteristics of the stack Determines the type of data stored in it.

Recommended tutorial: "Java Tutorial"

The above is the detailed content of What is a reference variable in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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