JAVA value passing and reference passing with stacks, heaps, and constant pools __java

Source: Internet
Author: User
Tags stringbuffer


Learn about the functions of each memory area of Java memory management above:

The basic concepts of stack and heap and constant pools are first understood in relation to Java value passing and reference passing:

Give me a chestnut:

String s = ' Lizhi ';

The internal implementation step of directly initializing a variable of type string: ①JVM first Initializes a string reference ② in the stack and then goes to the constant pool to find if there is a Lizhi string ③ if present, point the reference in the stack directly to the Lizhi of the constant pool, if it does not exist, A new Lizhi is created in the constant pool, and the reference in the stack points to the Lizhi.

String t = new string ("Newlizhi");

To initialize the internal implementation step of a string type variable by using NEW: ①JVM First Initializes a string reference in the stack ② then opens a new memory space in the heap and puts the Lizhi in ③ to point the reference in the stack to the memory address in the heap

public class C1215_1 {public
	static void Main (string[] args) {
		String a = "Lizhi";
		String a1 = "Lizhi";
		String b = new String ("Lizhi"); 
		StringBuffer C = new StringBuffer ("Lizhi");
		System.out.println (A = = A1);
		System.out.println (A = = B);
		Change (a,b,c);
		System.out.println ("A:" +a+ "\ n" + "B:" +b+ "\ n" + "C:" +c);
	public static void Change (String a,string b,stringbuffer c) {
		a = ' change ';
		b = "Change";
		C.append ("Change");
The result:

It can be seen from the above code; A and A1 are all pointing to the same memory address in the constant pool, so they are equal.

Then for value passing and reference passing, my understanding is that the so-called value-passing and reference-passing, is only a copy of the reference in the stack of this data;

The Li Zilai said: When the incoming argument is a, it is actually passed a copy of a point pointing (to "Lizhi" in the constant pool). A (copy) = "Change" The implementation step of the sentence is the same as the above initialization a: ① in a constant pool to find if there is a change in the string, point directly to it if it exists, and if it does not exist, create a new change and point to it; , the original constant pool of Lizhi are still there, and a is still pointing to Lizhi;

When the incoming argument is B, the basic steps are the same as above, but a little differently, B points to the heap, but the copy of B points to the constant pool.

When the incoming argument is C is, the incoming copy of C and C are all pointing to the memory address of the Lizhi in the stack, and when the C.append "Change" operation is performed, the Lizhi memory in its stack is operated directly, changing the data in the Lizhi memory address And because c also points to this memory, the method executes, and the data of the memory address that C points to is changed;

It follows that the difference between so-called value-passing and reference-passing is

Whenever you make a change to the data that points to a constant pool, you first go to the value you want to change, if it exists, point directly to it, and if it does not, create a new one and point directly to it. The result of this operation is that the original value still exists in the constant pool (the data in the constant pool is shared)

But for the data in the stack, it is directly in the corresponding memory address of the data changes, which will make the original data changes.

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: 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.