Breaking the Java Myth (2): The parameter is in the address

zhaozj2021-02-16  70

Get rid of the myth of java (b) of: cherami translation Friday, August 16 2002 10:18 AM rid of java Myth two: the parameters are pass-java in different newsgroups, the parameter is pass by value or site has been a frequently The topic of argument. The center of misunderstanding is the following two facts: the object is the parameter of the pass reference is the two of the pass values. Can these two can be established simultaneously? One word: Yes! In Java, you have never delivered an object, and you are only a reference to an object! In one sentence, Java is a biography. However, when you pass a parameter, there is only one parameter transfer mechanism: pass the value! Typically, when the programmer discusses the pass value and the transmission, they refer to the language parameter transfer mechanism, C support these two mechanisms, so the programmer used to use C starts like how the Java cannot be determined how to pass parameters . The Java language is simple to simply only support the mechanism of parameter transmission. There are two types of variables in Java: reference types and original types. When they are passed to the method as a parameter, they are all transmitted. This is a very important difference, the following code paradigm will illustrate this. We need to define a shortage value and pass before proceeding. The value means that the method or function received is a copy of the original value when the parameter is passed to a method or function. Therefore, if the method or function modifies the parameters, the affected is just a copy, the original value remains unchanged. The confusion about the parameters in Java is because many Java programmers are converted from C . C has a reference and non-class type variable, and is used by passing reference and biography. The Java language has the original type and object reference, then, according to logic, Java uses the pass value for the original type, just like C . After all, you will think of if you are passing a reference, it must be a reference. This is a very tempting idea, but it is wrong! In C and Java, when the parameters of the function are not referenced, you passed the worth copy (pass value). But it is different for the reference type. In C , when the parameter is a reference type, you passed the reference or memory address (pass reference), and in Java, the result of passing a reference type parameter is only the copy of the reference (pass value) instead of reference itself. This is a very important difference! Java does not consider the type of parameters, and it will pass the copy of the parameters. Still do not believe? If Java is a pass reference, the SWAP method in the following example will exchange their parameters. Because it is a pass value, this method is not working properly as expected.

Class swap {public static void main (string args []) {INTEGER A, B; INT I, J; a = new integer (10); b = new integer (50); i = 5; j = 9; system. Out.println (/ "before swap, a is /" a); system.out.println (/ "before swap, b IS /" b); swap (a, b); system.out.println (/ "After swap a is /" a); system.out.println (/ "after swap b is /" b); system.out.println (/ "before swap i is /" i); system.out .println (/ "before swap j is /" j); SWAP (i, j); system.out.println (/ "after swap i is /" i); System.out.Println (/ "after swap J IS / " J);} public static void swap (Integer IA, Integer IB) {INTEGER TEMP = IA; IA = IB; IB = Temp;} Public Static Void Swap (INT Li, INT LJ) {Int Temp = LJ; Li = LJ; LJ = Temp;}} The above program output is: Before Swap, A is 10 Before Swap, b IS 50AFTER SWAP A 10After Swap b IS 50 Before Swap i IS 5 Before Swap J IS 9AFTER SWAP I IS 5 After SWAP J IS 9 Because the SWAP method receives a copy (pass value) of the reference parameter, the modification of them will not reflect to the calling code.

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

New Post(0)