Java Essentials-Stacks, reference passing values, garbage collection, and more

Source: Internet
Author: User

In Java, is the object passed as a function parameter as a value pass or a reference pass? What is the difference between string str = "ABC" and string str = new String ("abc") on storage? Want to become a qualified to move a brick work, master it, tremble!

Heap and Stack (stack)

There are heaps and stacks in the data structure, and the concept of heap and stack in memory allocation, which refers to heap space and stack space, take care not to confuse the two , the latter is to be discussed here.

In data structures, heaps and stacks are two different kinds of data structures. Stack is a kind of last-in-first-out linear table, the heap is a special complete binary tree, the value of all non-terminal nodes in the tree is not greater than (or not less than) the value of their left and right child nodes.

In memory allocations, stacks and heaps (heap) are places where Java is used to store data in RAM . In C + +, programmers can request heap space through the malloc function, but in Java, programmers cannot directly manipulate heaps and stacks, which can only be managed automatically by Java.

Stack advantage: ① access faster than the heap, next to the register directly in the CPU, ② stack data can be shared. The size and lifetime of the data in the stack must be deterministic and lack some flexibility. The advantage of the heap is that the memory size can be allocated dynamically, and the lifetime does not have to tell the compiler beforehand that the Java garbage collector automatically collects the data that is no longer in use. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.

A classic metaphor for stacks and stacks: use stacks like we go to a restaurant to eat, just order (send an application), pay, and eat (use), eat enough to go, do not bother to cut vegetables, wash vegetables and other preparation work and washing dishes, brush pots and other finishing work, his advantage is fast, but less freedom. The use of the heap is like a DIY dish that you like to eat, more trouble, but more in line with their own tastes, and great freedom .

Java two types of data and storage

Java has two data types: ① is a basic data type, a total of 8 types, namely, int, short, long, byte, float, double, Boolean, char ( Note does not contain the string data type ); ② The other is the object data type, which is the class type or the application data type, such as Integer, String, double, and so on.

Java Essentials-Stacks, reference passing values, garbage collection, and more

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.