Java programming: Method calls Pass object arguments

Source: Internet
Author: User

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.

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.