Java Value delivery completely understand ~

Source: Internet
Author: User


















[ ] I don't remember how I learned it, but I'm sure most of the people in Java Learning Java have always thought a A small technical problem: Passing a method parameter in Java, an object is passing a reference, and the base data type is passing a value. And no one seems to have asked questions. Until recently, when I was writing an exam paper for the company's basic Java developers, I realized that this was wrong. In the method, the objects in the Java language are passing addresses, not references, and the two concepts are very different, and I believe that people familiar with C + + should know. For example: Suppose the object test has a property of name. 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 ()); } This time, you can see that the print is "ABC", not "CBA", because, in this call, it's equal to declaring two variable obj, T, pointing to the same address, calling the call method, just passing the address that obj points to T, Obj itself does not pass the past (that is, there is no pass-through reference), and when you reassign (that is, the object reference to other storage space), it only affects T, without affecting obj. Such a pass can only be referred to as an address pass, or a reference to an object pass, rather than a passing reference or a reference pass. I don't know whether this is a translation error or a mistake we understand. But such problems are clearly differentiated in C + + (through * and &)
time: 2004-4-06 13:18:27     Title: A Java technology problem that definitely harms a lot of people.




Time: 2004-4-06 21:07:10 title:
1, first to find out a problem: Java has no pointers.

Many people would rather not appear in Java than in C and C + +, which once brought us joy as well as infinite pain. In reality, however, Java has pointers, and identifiers for each object in Java (other than the basic data type) belong to one of the pointers. But their use is subject to strict restrictions and precautions, which are referred to in the book <thinking in Java> as a handle.

2. Pass Handle
When a handle is passed into a method, the same object is still pointed to.
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);
}

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 results:
Before Swap:a=com.lib.testit@16930e2 name:a
Swaping:a= com.lib.testit@16930e2 name:a

3, the passing of a handle can cause unexpected changes to 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 ("a");
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 results:
Before Swap:a=com.lib.testit@16930e2 name:a
After Swap:a=com.lib.testit@16930e2 name:b ' s name

We see that a is still the original a, but name is not the original name.
In the Swap () method, Swap1 and Swap2 each other, and this time swap2 points to a, so the name of a is changed at SetName (), not the name of B.

Why is that so?
Brother Liang_chen: The values in Java are actually copy references, not copy objects.

Summarize:
1: For a parameter of a value type, a copy of the value is passed.
2: For reference-type parameters, a copy of the reference itself is passed.
So the key depends on how you understand this "value" in the pass value.




Time: 2004-4-06 22:56:27 title: Haha, good lively
It never occurred to me that the problem resonated with so many friends,
There's a guy upstairs about "Java delivery is a reference copy, not a reference itself, not an object." "That's a very good saying. Unfortunately, none of the books found today have been explained.
Of course, this pass is an object, not a simple data type, a simple data type, or a copy of a value.
There's another guy upstairs. The compiler principle, which is where the problem is, passing the address, passing the reference, passing the result (that is, the value copy), and here is the difference between the passing reference and the delivery address.
The understanding of the Liang_chen upstairs is very good, I until the first part of the real discovery (around my side is almost not clear this problem), more than three years of Java Analysis design and development experience, to today to understand the basis of such a problem, I really embarrassed. :(
Therefore, other still in the debate is not clear friends, we must seriously learn Java, can not casually say that their proficient, haha, we explore more, continue to improve together.



time: 2004-11-10 12:38:00     title: Handle and pointer--on the question of Phoenix dance
Phoenix's original post is really too long, here is a new post to discuss this issue.

I think that Java does not have the concept of pointers and addresses in C + +, it has only the concept of a handle (handler). Also take the Phoenix dance two methods for 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 were built, assuming that the main method builds an object of object 1, the call method builds an object of Object 2,
in the main method, the variable obj gets the handle to "Object 1", and
in the parameter pass, Variable obj passes this handle to the variable T,
in the call method, the variable t first changes the property of Object 1, and then the variable t gets the handle to "Object 2" (but obj is still a handle to object 1), and the
call method returns after the "Object 2" Lost the only handle, inevitably into the garbage collector's line of sight. Obj is still a handle to object 1, and because the properties of object 1 have been reset, we can see that the printed result is "ABC".

So I think the following assertion is OK:
"pass the method parameter in Java, the object is a pass-through reference, and the base data type is the passed value." The

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.