public static void main (String [] args) {
String [] STR={" a ", "b", "c", "d"};
Test1 (STR);
System.out.println(str);
System. The out. Println (Arrays. ToString (STR));
}
Public static void test1 (String [] a) {
String [] b={" 1 ", "2"};
System.out.println(b);
a=b;
}
CodePudding user response:
Can you explain it with memory map? Introduced into two dimensional array can change the value of a one-dimensional array strange inside!CodePudding user response:
You test1 method, a=b, directly to the address of a to b, in the main method, STR point or the original address, you can't directly by "=" in a way to change, the change of address is not valueCodePudding user response:
You can for loop assignment or System. Arraycop methodCodePudding user response:
Qing search pass Java method value/referenceThis can speak more clear
Simple, the method of the parameter passed is a copy of the object address, so you change the content of the copy is the method in vitro does not have any effect, if you modify address pointing to the content of the object, it can be seen.
For example, I have a map, when you need, I give you a copy, then you to tamper with the contents of the map, actually does not have any effect on ontology, but you go directly to the map corresponding to decorate the house, that have an impact.
CodePudding user response:
, of course, the basic types and reference types method is passed on in fact is essentially the same, just the basic types of values is itself, there is no point to the object, the reference type is a memory addressCodePudding user response:
Public static void main (String [] args) {String [] STR={" a ", "b", "c", "d"};
STR=test1 (STR);
System.out.println(str);
System. The out. Println (Arrays. ToString (STR));
}
Public static String [] test1 (String [] a) {
String [] b={" 1 ", "2"};
System.out.println(b);
A=b;
return a;
}
CodePudding user response:
Over an object, you modify the properties of the object, will affect the original object, but you=another object directly, with the original object that doesn't matter,For arrays, you can modify the values in the array affect the original object, two-dimensional arrays can be modified, that is because you are the one dimensional array in the two-dimensional array, that is, the value of the two dimensional array, instead of two dimensional array=again
CodePudding user response:
This is a simple Java memory model analysis problem, main is to grasp the Java memory model has a certain degree
CodePudding user response:
Actually you just change a, this is a memory of a local variable, the stack and STR point is a variable of heap, STR and a relationship is a bit, STR gave its address values to a, no matter how to change the value of a, will change the value in the STR