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
- The virtual machine is assigned a memory address of NUM and a value of 0 is saved.
- The virtual machine replicates a num, and we call him num ', num ' and Num's memory addresses, but the value is 0.
- The virtual machine speaks num ' incoming method, and the method changes the value of NUM to 1.
- 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:
- The virtual machine opens up a product's memory space in the heap, with Proname and num in memory.
- The virtual machine is assigned p a memory address in the stack, which is stored in the memory address of product 1.
- 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.
- The P ' Pass method changes the Proname and num in 1.
- 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:
- The virtual machine opens up a piece of memory in the heap, and the value "AB" exists.
- The virtual machine allocates a memory to STR in the stack, and the address in 1 is stored in memory.
- Virtual machine copies a copy of STR, we call str ', str and str ' memory different, but the stored value is 1 address.
- The Str ' Incoming method body
- The method body creates a piece of memory in the heap, and the value "CD"
- Method body changes the value of STR to 5 memory address
- 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