Stack memory and heap memory in Java

Source: Internet
Author: User

Why do we often say that a primitive type passes a specific value, whereas an object passes an object's memory address? To figure out this problem, you need to figure out the stack memory and heap memory.

Java memory is divided into stacks of memory and heap memory, the role of the two are different, we can simply understand the following:

When we create a variable of a Java primitive type, only the stack memory is used and the heap memory is not used, and the content stored in the stack memory is the value of the base type.

int a = 3;
int b = 3;

Executing the first line int a = 3, the JVM will first create a reference to a variable, and then in the stack memory to find whether there is already a value of 3, not found, will be in the stack memory to open an area to store 3, and then point A to the location of storage 3. The second line of int b=3 is then executed, and when the value of 3 is found in the stack memory, the reference to variable B also points to the location where the 3 is stored.

When we use new to create an object of a non-basic type, we not only use the stack memory but also the heap memory.

For example, Student s=new Student ();

Also first in the stack memory to open up a piece of memory, with S point to this memory, but then the stack memory is not new Student (), but only a memory address, this memory address is in the heap memory of the memory of the new Student () The first address of the block.

With the concept above, you can solve some simple problems.

First, we all know that the content of the comparison string is used by equals (), not = =, for what reason?

String S1=new string ("abc");

String S2=new string ("abc");

The result of S1==s2 is false, because the first and second rows are used to create new objects, and the first address of the two objects in the heap memory must be different, so the contents of the stack memory pointed to by S1 and the contents of the stack memory pointed to by S2 must be different, and = =

Compared to the stack memory, this is definitely not equal.

The Equals method can be understood as comparing the contents of the heap memory, since the contents of the two heap memory are all ABC, it is necessarily equal.

The second one, change the top slightly.

String s1= "ABC";

String s2= "ABC";

At this time s1==s2, because there is no new, there is no allocation of heap memory, are used is the stack memory, in the second row, the stack memory already exists in the ABC, then s2 directly point to this address on it, so there will be s1==s2.

The third one, executes String s1= "Hello"; what's the result of s1=s1+ "world"?

When the first sentence is executed, a piece of memory is allocated in the memory of the stack, and a new memory is allocated to Hello World when the second sentence is executed, while the S1 points to the memory of Hello World, which is no longer the memory of hello. The memory that stores hello will be reclaimed by the garbage collector.

Fourth, we often say that when copying a basic type is passed a specific value, while copying the object is passed the memory address of the object, when the stack memory and heap memory understand, you can understand why.

Add:

String a= "ABC";
String b= "ABC";
String C=new string ("abc");
String D=c.intern ();

Because the reference variable C points to the address of the heap memory; intern (); But in the stack opened up a piece of memory; in fact C.intern (); When executing, the object "ABC" is found in the stack memory, so the pointer is directed to "ABC", so that a = = d, the b==d result is true;

Stack memory and heap memory in Java

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.