String is a value pass or a reference pass

Source: Internet
Author: User

Solve a bug today and find a place where it's easy to make a mistake. I passed a string parameter to the method and changed the referenced value within the method. Then use this value outside of the method to find out whether the string or the previous value has not changed.

Java is divided into value passing and reference passing at the time of parameter transfer. A value is passed when the argument is a base type, and a reference is passed when the parameter is a wrapper type. For example:

Basic type parameters
 Public class Test {    publicstaticvoid  main (string[] args) {        int num = 0 ;        Changenum (num);        System.out.println ("num=" +num);    }      Private Static void changenum (int  num) {        = 1;    }}

The result of the printing is num=0 .

Package Type parameters
 Public classTest { Public Static voidMain (string[] args) {Product P=NewProduct (); P.setproname ("Before"); P.setnum (0);        Changeproduct (P); System.out.println ("P.proname=" +p.getproname ()); System.out.println ("P.num=" +p.getnum ()); }     Private Static voidchangeproduct (Product p) {P.setproname ("After"); P.setnum (1); }} classProduct {Private intnum; PrivateString Proname;  Public intGetnum () {returnnum; }      Public voidSetnum (intnum) {         This. num =num; }      PublicString Getproname () {returnProname; }      Public voidsetproname (String proname) { This. Proname =Proname; }}

The result of the operation is: p.proName=after and p.num=1 .

The two examples above are obvious value passing and reference passing. But what if the argument is of type string? Let's take a look at specific examples:

 Public class Test {    publicstaticvoid  main (string[] args) {        = "AB";        Changestring (str);        System.out.println ("str=" +str);    }      Private Static void changestring (String str) {        = "CD";    }}

Guess what the running result is? According to the previous example, string should be a package type, it should be a reference pass, it is possible to change the merit, the result of running should be "CD". Let's actually run a look,

str=ab, how does that explain? Is string a basic type? It doesn't even speak.

This is going to start with the Java underlying mechanism, where the Java memory model is divided into heaps and stacks .

1.基本类型的变量放在栈里;2.封装类型中,对象放在堆里,对象的引用放在栈里。

when a method passes a parameter, Java copies a copy of the variable and passes it to the method body to execute. This sentence is difficult to understand, but also to explain the essence of the problem. Let's start by saying the basic types of delivery

  1. The virtual machine is assigned a memory address of NUM and a value of 0 is saved.
  2. The virtual machine replicates a num, and we call him num ', num ' and Num's memory addresses, but the value is 0.
  3. The virtual machine speaks num ' incoming method, and the method changes the value of NUM to 1.
  4. Method ends, the value of num is printed outside the method, because the value in NUM memory does not change, or 0, so printing is 0.

We explain the delivery of the encapsulation type again:

  1. The virtual machine opens up a product's memory space in the heap, with Proname and num in memory.
  2. The virtual machine is assigned p a memory address in the stack, which is stored in the memory address of product 1.
  3. The virtual machine has copied a p, we call him P ', p and p ' memory addresses, but they have the same value, which is the memory address of product in 1.
  4. The P ' Pass method changes the Proname and num in 1.
  5. Method ends, the method prints the value of the variable in P, because p and p ' are stored in the address of product in 1, but the value of product in 1 has changed, so the method to print the value of P is after the method executes. The effect we see is that the value of the package type is changed.

Finally, let's explain the steps of string in the delivery process:

  1. The virtual machine opens up a piece of memory in the heap, and the value "AB" exists.
  2. The virtual machine allocates a memory to STR in the stack, and the address in 1 is stored in memory.
  3. Virtual machine copies a copy of STR, we call str ', str and str ' memory different, but the stored value is 1 address.
  4. The Str ' Incoming method body
  5. The method body creates a piece of memory in the heap, and the value "CD"
  6. Method body changes the value of STR to 5 memory address
  7. Method End, method print out str, because STR is stored in 1 address, all printing results are "AB"

In this way, we understand the whole process of Java in method parameters. In fact, the above sentence is more important. when a method passes a parameter, Java copies a copy of the variable and then passes it to the method body to execute.

Transfer from https://www.cnblogs.com/boboooo/p/9066831.html

String is a value pass or a reference pass

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.