Java transmission is a copy of the reference, neither the reference itself nor the object.

Source: Internet
Author: User
Code
public class Testit {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public static void main(String[] args) {
Testit a = new Testit();
a.setName("a");
Testit b = new Testit();
b.setName("b");
System.out
.println("before swap: " + "a=" + a + " name: " + a.getName());
swap(a, b);
System.out
.println("before swap: " + "a=" + a + " name: " + a.getName());
}

private static void swap(Testit swap1, Testit swap2) {

Testit temp;
temp = swap1;
swap1 = swap2;
swap2 = temp;
swap1.setName("a's name");
swap2.setName("b's name");
}

}

1. First, find out the question: is there a pointer in Java?

In C and C ++
There are also infinite and painful pointers. Many people prefer not to appear in Java any more. However, in fact, Java has pointers, and every object (except the basic data type) in Java is identified.
All belong to a pointer. However, their use is strictly restricted and prevented. They are called handles in <thinking in Java>.

2. Transfer handle
When the handle is passed into a method, it still points to the same object.
Public class testit {
Private string name;

Public String getname (){
Return name;
}

Public void setname (string name ){
This. Name = Name;
}

Public static void main (string [] ARGs ){
Testit A = new testit ();
A. setname ("");
Testit B = new testit ();
B. setname ("B ");
System. Out. println ("before swap:" + "A =" + A + "name:" + A. getname ());
Swap (A, B );
}

Private Static void swap (testit swap1, testit swap2 ){
System. Out. println ("swaping:" + "A =" + swap1 + "name:" + swap1.getname ());
Testit temp;
Temp = swap1;
Swap1 = swap2;
Swap2 = temp;
}

}

Output result:
Before swap: A = com. Lib. testit @ 16930e2 name:
Swaping: A = com. Lib. testit @ 16930e2 name:

3. Passing a handle will accidentally change the caller's object.
Public class testit {
Private string name;

Public String getname (){
Return name;
}

Public void setname (string name ){
This. Name = Name;
}

Public static void main (string [] ARGs ){
Testit A = new testit ();
A. setname ("");
Testit B = new testit ();
B. setname ("B ");
System. Out. println ("before swap:" + "A =" + A + "name:" + A. getname ());
Swap (A, B );
System. Out. println ("after swap:" + "A =" + A + "name:" + A. getname ());
}

Private Static void swap (testit swap1, testit swap2 ){
Testit temp;
Temp = swap1;
Swap1 = swap2;
Swap2 = temp;
Swap1.setname ("A's name ");
Swap2.setname ("B's name ");
}

}

Output result:
Before swap: A = com. Lib. testit @ 16930e2 name:
After swap: A = com. Lib. testit @ 16930e2 name: B's name

We can see that A is still the original A, but the name is not the original name!
In the swap () method, swap1 and swap2 exchange each other. In this case, swap2 points to a. Therefore, when setname () is used, the name of a is changed, instead of B's name.

Why?
Liang_chen: The value passed in Java is actually a copy reference, not an object.

Summary:
1: For a value type parameter, the copy of the value is passed.
2: For parameters of the reference type, the copy of the reference itself is passed.
So the key is how you understand the "value" in the passed value.

Related Article

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.