search
HomeCommon ProblemStack difference

Stack difference

Jun 03, 2019 pm 02:07 PM

Stack difference

The difference between heap and stack:

1. The difference between stack space allocation:

1. Stack (operating system): automatically allocated and released by the operating system to store function parameter values, local variable values, etc. Its operation method is similar to the stack in the data structure;

2. Heap (operating system): It is generally allocated and released by the programmer. If the programmer does not release it, it may be recycled by the OS when the program ends. The allocation method is similar. in the linked list.

2. Differences in stack caching methods:

1. The stack uses the first-level cache. They are usually in the storage space when they are called, and immediately after the call is completed. Release;

2. The heap is stored in the second-level cache, and the life cycle is determined by the garbage collection algorithm of the virtual machine (not that it can be recycled once it becomes an orphan object). Therefore, the speed of calling these objects is relatively low.

Heap: In memory, reference data types are stored. The size of reference data types cannot be determined. The heap is actually a storage space in a linked list structure that uses scattered space in the memory. The size of the heap is determined by The size of the reference type is directly determined. Changes in the size of the reference type directly affect the changes in the heap

Stack: It is a value type stored in the memory. The size is 2M. If it exceeds, an error will be reported and the memory will overflow

3. Differences in stack data structure:

Heap (data structure): The heap can be regarded as a tree, such as: heap sort;

Stack (data structure): A first-in, last-out data structure.

Features: first in, last out

For first in, last out, because the underlying implementation of Linklist is a linked list structure, we use Linklist to explore what is first in, last out

The code is as follows:

package com.zking.list;
 
import java.util.LinkedList;
 
public class Linklisttest {
 
public static void main(String args[]) {
LinkedList ll = new LinkedList();
for (int i = 0; i < 5; i++) {
ll.addFirst(i);
}
 
ll.removeFirst();
 
for (Object object : ll) {
System.out.println(object);
}
 
}
 
}

Running result:

Stack difference

According to this simple example, we can see that through In the for loop, the order in which we add data to the collection is 0, 1, 2, 3, 4,

. When the delete method is executed, 4 is deleted, and the final traversal result is 3, 2, 1, 0.

So it reflects the characteristics of the stack: first in, last out. .

The above is the detailed content of Stack difference. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.