A detailed explanation of objects, object references, heaps, stacks, value passing, and reference passing in Java

Source: Internet
Author: User

a detailed explanation of objects, object references, heaps, stacks, value passing, and reference passing in Java
1. Differences in object and object references:
(1). Object:
All things are objects. An object is an instance of a class.

In Java, new is used to create objects on the heap.

An object can be pointed to by multiple references.
(2). Object reference:
Similar to addresses in C + +. objects can be found through object references.

A reference can point to multiple objects. The manipulated identifier is actually a reference to the object.


It's like: objects are stored in the house, and the reference to the object is the key to the house.
2. Differences in value passing and reference passing:
(1). Value passing: a copy of the value is passed.

That means they don't close each other after they're delivered.
(2). Reference passing: a reference to a value is passed.

Is the address of the memory space.

This means that both before and after delivery point to the same reference (that is, the same memory space).
3. Heap and Stack differences:
(1). Stack: Some basic types of variables defined in the function. and object reference variables are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates memory space for the variable in the stack. When the scope of the variable is exceeded. Java will voluntarily release the allocated memory space for that variable.

This memory space can be used for other purposes immediately.

A reference variable is equivalent to a name that is an array or an object. You can then use reference variables from the stack in your program to access arrays or objects in the heap.
(2). Heap: heap memory is used to hold objects and arrays created by new. The memory allocated in the heap is managed by the Java Virtual machine's own active garbage collector.
(3). Life cycle:
A. Life cycle of reference variables: program execution is released beyond the scope of the variable
B. Arrays and Object lifecycles: the memory that the array and the object itself occupy will not be freed except by the code block where the program executes the statements that produce the array or object using new. Arrays and objects become garbage and can no longer be used when no reference variable points to it.

However, the memory space is still occupied and is then collected by the garbage collector at an indeterminate time.

That's why Java is more of a memory, and actually the variables in the stack point to the variables in the heap memory, which is the pointer in Java.


memory mechanism in 4.Java:
Java divides memory into two types: one is stack memory.

One is heap memory.


Stack Memory: A reference variable for some basic types of variables and objects defined in the function.
Heap Memory: object and array created by new. After creating an array or object in the heap, you can also define a special variable in the stack. Make the value of this variable in the stack equal to the first address of the array or object in the heap memory.

This variable in the stack becomes the reference variable for the array or object. You can then use reference variables from the stack in your program to access arrays or objects in the heap, which is the equivalent of a name for an array or object. A reference variable is a normal variable that is allocated in the stack when defined.

A detailed explanation of objects, object references, heaps, stacks, value passing, and reference passing in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.