The following are 2 tests, which test modifiable object and unmodified
/** The trap of the object parameters inside the
* java
*
* @author old Zi Zhu's home (laozizhu.com)
*
*/
public class Test {
Public s tatic void Main (string[] args) {
TestValue TV = new TestValue ();
Tv.first ();
Testinteger ti = new Testinteger ();
Ti.first ();
}
}
class TestValue {
class Value {
public int i =;
}
//Initialization
Value v = new value ();
public void First () {
//is of course
System.out.println (V.I);
//First Call
second (v);
System.out.println (V.I);
Third (v);
System.out.println (V.I);
}
public void second (Value v) {
//At this point v is a local variable
//And the Class property's V Equality
System.out.println (v = = this.v);
V.I = 20;
}
public void third (Value v) {
//Reset an object
V = new Value ();
//At this point the V is also a local variable
//But it is not equal to the V of the class attribute
//Modify the value of this V-pointing object, which has no effect on the attribute V in the class.
System.out.println (v = = THIS.V);
V.I = 25;
}
}
class Testinteger {
//initialization
Integer v = new Integer;
public void First () {
//1 of course)5
System.out.println (v);
//(First call
second (v);
System.out.println (v);
Third (v);
System.out.println (v);
}
public void second (Integer v) {
//At this point v is a local variable
//and the Class attribute's V equality
System.out.println (v = = this.v);
/ /But this sentence differs from the previous one, although it is also assigned to a reference, but because the integer is not modifiable
//So a new object is generated here.
V = 20;
//Of course, they are no longer equal
System.out.println (v = = this.v);
}
public void third (Integer v) {
//Reset an object
V = new Integer;
//At this point the V is also a local variable
//But it is not equal to the V of the class attribute
//Modify the value of this V-pointing object, which has no effect on the attribute V in the class.
System.out.println (v = = THIS.V);
}
}
Run results
15
true
20
false
20
15
true
false
15
false
15
Hopefully this example will unlock some beginners ' questions.