Java value Transmission ~

Source: Internet
Author: User
Time: 13:18:27 title: a Java technical problem that has definitely hurt many people!
I don't remember how I learned it, but I believe that most people who are engaged in Java learning think this is a small technical problem:
In Java, the method parameter is passed, the object is passed reference, and the basic data type is passed value. And it seems that no one has raised any questions.
It wasn't until recently that I was writing test papers for the company's basic java developers that I found this error! In methods, objects in the Java language pass addresses rather than references. These two concepts are very different. I believe that anyone familiar with C ++ should know.
For example, assume that the object test has the name attribute.
Public void call (test T ){
Test t2 = new test ();
T2.setname ("CBA ');
T. setname ("ABC ");
T = t2;
}

Public static void main (string [] Arg ){
Test OBJ = new test ();
Call (OBJ );
System. Out. println ("OBJ" + obj. getname ());
}
At this time, you can find that the printed content is "ABC", not "CBA". The reason is that in this call, the two variables OBJ, T, are declared, they point to the same address, call the call method, but pass the address pointed to by OBJ to T, and OBJ itself does not pass the past (that is, no reference is passed ), when you assign a value again (that is, point the object reference to another bucket), it means that only T is affected, but obj is not affected.
Such a transfer method can only be referred to as address transfer, or the transfer of reference objects, rather than passing references or references.
I don't know whether this is a translation error or an error we understand. However, such problems are clearly distinguished in C ++ (through * and &)

Time: 21:07:10 title:
1. First, find out the question: is there a pointer in Java?

Many people would rather not appear in Java as a pointer to the joy and infinite pain that has been brought to us in C and C ++. However, Java actually has pointers. The identifiers of every object (except the basic data type) in Java belong to the pointer type. 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.

Time: 22:56:27 title: Haha
I did not think that this problem aroused the resonance of so many friends,
There was a senior friend upstairs who said, "Java transmission is a copy of the reference, neither the reference itself nor the object ." This sentence is very good! Unfortunately, none of the books found today have been explained.
Of course, this transfer is an object, not a simple data type, but a copy of the transmitted value.
There is another guy upstairs who has come up with the compilation principle. This is exactly the problem: passing the address, passing the reference, and passing the result (that is, copying the value ), what is the difference between the transfer reference and the transfer address?
The liang_chen upstairs has a very good understanding. I did not find it until the previous paragraph (I am not sure about the problem). I have more than three years of Java analysis, design, and development experience, I am so ashamed to understand such a basic problem. :(
Therefore, other friends who are still not clear about the argument must study Java carefully and cannot talk about what they are proficient in. Haha, everyone should discuss more and continue to improve together.

Time: 12:38:00 title: handle and pointer-discuss the issue of fengwu Yunyang
The original post of Feng WuYang is too long. I will open a new post here to discuss this issue.

In my opinion, Java does not have the pointer and address concept in C ++. It only has the handler concept. Take the following two methods as an example:

Public void call (test T ){
Test t2 = new test ();
T2.setname ("CBA ');
T. setname ("ABC ");
T = t2;
}

Public static void main (string [] Arg ){
Test OBJ = new test ();
Call (OBJ );
System. Out. println ("OBJ" + obj. getname ());
}

A total of two test objects are constructed. Assume that the object constructed by the main method is "Object 1", and the object constructed by the call method is "Object 2 ",
In the main method, the variable OBJ obtains the handle of "Object 1,
In parameter transfer, the variable OBJ passes the handle to the variable t,
In the call method, variable t first changes the attribute of object 1, then the variable t obtains the "Object 2" handle (but obj is still the "Object 1" handle ),
After the call method is returned, the unique handle of object 2 is lost, which inevitably leads to the sight of the garbage collector. OBJ is still the handle of "Object 1". Because the property of "Object 1" has been reset, we can see that the printed result is "ABC ".

Therefore, I think the following statements are correct:
"In Java, the method parameter is passed, the object is passed reference, and the basic data type is passed value ."

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.