Java value passes thoroughly understand ~

xiaoxiao2021-03-06  70

Time: 2004-4-06 13:18:27 Title: A Java technology issue that absolutely harms a lot of people!

I don't remember how I learned it, but I believe that most people who engage in Java learning Java have been thinking as a small technical issue: the passage of the method of the method of the Java, the object is the transfer reference, the basic data type is transmitted value. And it seems that no one has filed doubts. Until recently, I found out that this is wrong when I write a test paper for the basic Java developers of the company. In the method, the object passes the address in the Java language, not reference, these two concepts are very different, I believe that people who are familiar with C should know. For example, the following: assume that the object TEST has the properties of the 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 ());} At this time, you can find that the printed is "ABC", not "CBA" ", Because in this call, it is equivalent to declaring two variables Obj, T, which point to the same address, call the CALL method, just pass the address pointing to the OBJ to T, and Obj itself Did not pass the past (that is, no delivery), when you re-assign the value (i.e., point the object reference to other storage space), is equal to only affecting T, but does not affect the OBJ. This kind of delivery can only call it. Pass, or reference object passes, and is not tenderly to pass the reference or reference delivery. I don't know if this is an error in translation, or the mistake we understand. But this problem is obvious in C (through *versus&)

Time: 2004-4-06 21:07:10 Title:

1, first figure a question: Is there a pointer in Java? For us, there is also an unlimited painful pointer for us, and many people prefer it to appear in Java. In fact, in fact, Java is a pointer, and an identifier of each object in Java (except for the basic data type) belongs to a pointer. But their use has been strictly restricted and presented, and they are called handles in . 2. When the handle passes the handle into a method, the pointing is still 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 ("a"); test 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: Before swap: a=com.lib.testit@16930E2 name: a swaping: a = COM. Lib.Testit@16930E2 name: A 3, the transfer of a handle will make an unexpected change in 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"); test 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 ()); 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: Before swap: a= ​​a=, we see, but Name is not the original Name, but Name is not the original name. ! In the swap () method, SWAP1 and SWAP2 are converted to each other. At this time, SWAP2 points to A, so changed when setName () is a Name of A, instead of B Name. Why is this so? Liang_chen brother is high: the pass value in Java is actually a copy reference, not a copy object. Summary: 1: For the value of the value type, the pass is the value of the value. 2: For the reference type parameters, the passed is the copy of the reference itself. So the key wants to see how you understand this " Value. Time: 2004-4-06 22:56:27 Title: Haha, so lively

I didn't expect this problem to resonate so many friends. I have a brother upstairs about "Java delivery is a copy of the reference. It is neither the reference itself, it is not an object." This sentence is very good! " Unfortunately, any book discovered today is not explained. Of course this transmission is an object, not a simple data type, simple data type or copy of the transfer value. There is also a friend upstairs to take out the compilation principle, this is the problem, delivery address, transfer reference, passing result (that is, value copy), what is the difference between the delivery reference and delivery address? The Liang_Chen's understanding is very good. I really found it until the previous paragraph (I don't know this problem in my side), more than three years of Java analysis design and development experience, to today understand the problem, I It's really sweat. :( So, other friends who are still unclear, be sure to learn Java seriously, can't just say that they are proficient, haha, everyone can discuss, continue to increase together. Time: 2004-11-10 12:38:00 Title: Handle and Pointer - Discuss the problem of phoenix

The original post of Feng Dao Huang is too long, here is a new post to discuss this problem. I think Java does not have a C pointer, the concept of address, which only has the concept of handler. Two ways to be a phoenix dance is 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 ());} Two Tests Object, assuming that the object constructed by the main method is "object 1", the Call method is built into "object 2", in the main method, the variable OBJ obtains the handle of "object 1", in the parameter transfer, the variable OBJ This handle passes to the variable T. In the CALL method, the variable T first changes the properties of "object 1", then the variable T has obtained the handle of "object 2" (but Obj is still "object 1" handle), Call After the method returns, since "object 2" lost the unique handle, it is inevitable to enter the trash collector's sight. Obj is still the handle of "object 1", because "object 1" is reset, so we It can be seen that the result of printing is "ABC". So I think the following arguments are no problem: "In the Java method, the object is passed, the object is the transfer reference, the basic data type is the transfer value. "

转载请注明原文地址:https://www.9cbs.com/read-86667.html

New Post(0)