Detailed explanations of objects, object references, heaps, stacks, value passing, and reference passing in Java

Source: Internet
Author: User

detailed explanations of objects, object references, heaps, stacks, value passing, and reference passing in Java
1. Differences between 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:
is similar to an address 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 Delivery:A copy of the value is passed. That means they don't close each other after they're delivered.
(2). Reference Delivery: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 that are 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 automatically frees the allocated memory space for that variable. The 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 the 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 automatic garbage collector of the Java Virtual machine.
(3). Life cycle:
A. The life cycle of a reference variable:program runs until the scope of the variable is released
B. The life cycle of arrays and objects:The memory that is occupied by the array and the object itself is not freed until the program runs to the block of code that uses new to produce the array or object's statement. Arrays and objects become garbage and can no longer be used when no reference variable points to it. However, it still occupies memory space and is collected by the garbage collector at a later indeterminate time. This is the reason why Java compares memory, in fact, 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 that defines some basic types of variables and objects in a function.
Heap Memory:Objects and arrays created by new. After creating an array or an 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, and in the future you can use the reference variable in the stack to access the array or object in the heap, which is the equivalent of a name that is an array or object. A reference variable is a normal variable that is allocated in the stack when defined.

Detailed explanations 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.