Home > Back-end >  The String class based applications, consult with code
The String class based applications, consult with code

Time:11-21

Small program: exchange method swap string a and b, pointing to the sample, why not exchange a success?
 public static void swap1 (String a and String b) {//exchange a and b 
String temp=b;
B=a;
A=temp.
System. The out. Println (a + "" + b);//output A, B
}
Public static void swap2 (String [] s) {
String \ [0]=s;
S [0]=s [1];
S [1]=temp;
}
Public static void main (String [] args) {

String a="a".
String b="b";
String [] s={" 1 ", "2"};
Swap1 (a, b);
Swap2 (s);
System. The out. Println (a + "" + b);//here why output A B? The String class is final types of reference data types, method isn't swap1 transfer reference? Why not output A B?
System. The out. Println (s [0] + "" + s [1]).//output 2 1, here I can understand
}

CodePudding user response:

Array variable s reference {" 1 ", "2"} array object, methods swap2 is the reference, the book says the String and reference data types, variables, a reference to "a" String objects, variable reference b "b" String objects, swap1 also is the reference, but the output is not the case, is not the whole of me? .

CodePudding user response:

In reference type as a parameter, the pass is a copy of address values, but the two address point to the same place, no changes in copy address point, to perform operations on data copy address to affect the value of the original data
String is special, his is final, it is the underlying a final type of ch [] array, it is immutable, every time you make changes to it is led it to a new object, it is the value of the original address change

CodePudding user response:

This article https://www.cnblogs.com/jagh/p/jjjj.html for reference

CodePudding user response:

Like nine swords dugu finale: Java pass parameters, the method is to copy a variable, and then the incoming method body to carry out,

Under the simple analysis:
1, the String a="a"; --> In the JVM heap heap1 opened up A piece of memory, memory value "A", in the stack is allocated to A A memory walks, walks in storing the addresses of heap1
2, the swap1 (a, b); --> JVM is a copy of a, called a2, and assigned a memory stack2, deposit is also the address of the heap1, a2 into the method swap1
3, is actually in the way of a2, so the method is out of print a2
4, after the method, method to print a, since the store is heap1 address, or a

String b="b"; Similarly

CodePudding user response:

String is special, can't exchange, consider using StringBuffer as a parameter,

CodePudding user response:

Change your code, you can generate the results you want
Public static void swap1 (String a, String b) {//exchange a and b
String temp=b.i ntern ();
B=Anderson ntern ();
A=temp.
System. The out. Println (a + "" + b);//output A, B
}

CodePudding user response:

4th floor say very understand,

CodePudding user response:

reference 4 floor shpery response:
like nine swords dugu finale: Java pass parameters, the method is to copy a variable, and then the incoming method body to carry out,

Under the simple analysis:
1, the String a="a"; --> In the JVM heap heap1 opened up A piece of memory, memory value "A", in the stack is allocated to A A memory walks, walks in storing the addresses of heap1
2, the swap1 (a, b); --> JVM is a copy of a, called a2, and assigned a memory stack2, deposit is also the address of the heap1, a2 into the method swap1
3, is actually in the way of a2, so the method is out of print a2
4, after the method, method to print a, since the store is heap1 address, or a

String b="b"; Similarly
hello, I can understand your analysis,
According to this analysis, the exchange of String [] s={1, 2}, swap2 (String [] s), the method is a copy of the operation in the body s', so the print in the method body s: 1, 2
Back to the main, however, should be in the original s} {1, 2, but the result is

CodePudding user response:

refer to 6th floor KeepSayingNo response:
change your code, you can generate the results you want
Public static void swap1 (String a, String b) {//exchange a and b
String temp=b.i ntern ();
B=Anderson ntern ();
A=temp.
System. The out. Println (a + "" + b);//output A, B
}

Method the body right, but the main method to print the results or A, B,

CodePudding user response:

references 9 f chenping59 response:
Quote: refer to the sixth floor KeepSayingNo response:
change your code, you can generate the results you want
Public static void swap1 (String a, String b) {//exchange a and b
String temp=b.i ntern ();
B=Anderson ntern ();
A=temp.
System. The out. Println (a + "" + b);//output A, B
}

Method the body right, but the main method to print the results or A, B,

Simple point, is impossible for a String by changing the parameter to change, because the String is immutable, you can only change reference address

CodePudding user response:

reference 4 floor shpery response:
like nine swords dugu finale: Java pass parameters, the method is to copy a variable, and then the incoming method body to carry out,

Under the simple analysis:
1, the String a="a"; --> In the JVM heap heap1 opened up A piece of memory, memory value "A", in the stack is allocated to A A memory walks, walks in storing the addresses of heap1
2, the swap1 (a, b); --> JVM is a copy of a, called a2, and assigned a memory stack2, deposit is also the address of the heap1, a2 into the method swap1
3, is actually in the way of a2, so the method is out of print a2
4, after the method, method to print a, since the store is heap1 address, or a

String b="b"; In the same way

 public static void changeString (String a) {
After a=new String (" ");
}
Public static void changeArray (String [] s) {
S [0]=new String (" after ");
}
Public static void main (String [] args) {
String a="before";
String [] s={" before "};
ChangeString (a);
ChangeArray (s);
System. The out. Println (a);//output before
System. The out. Println (s [0]);//output after, why?
}

CodePudding user response:

refer to the second floor of green _ reply:
in reference type as a parameter, the pass is a copy of address values, but the two address point to the same place, no changes in copy address point, to copy the address to the data operation will affect the value of the original data
String is special, his is final, it is the underlying a final type of ch [] array, it is immutable, every time you make changes to it is led it to a new object, it is the value of the original address cannot be changed
 public static void changeString (String a) {
After a=new String (" ");
}
Public static void changeArray (String [] s) {
S [0]=new String (" after ");
}
Public static void main (String [] args) {
String a="before";
String [] s={" before "};
ChangeString (a);
ChangeArray (s);
System. The out. Println (a); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related