In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces what the semantics of passing by value in Java applications is. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Passing semantics by value in Java applications
Excerpt understanding that parameters are passed by value rather than by reference shows that Java applications have and only one parameter passing mechanism, that is, passing by value. It was written to debunk the common myth that Java applications pass parameters by reference to avoid common programming errors caused by relying on the behavior of passing by reference.
Some of the feedback on this excerpt is that I have confused the issue or got it completely wrong. In this column I will use C++ and the Java app to further clarify some facts.
Keystone
After reading all the comments, the question finally became clear, and there was confusion on at least one major issue. Some comments think that my excerpt is wrong because the object is passed by reference. Objects are indeed passed by reference; the excerpt does not conflict with this. The excerpt says that all parameters are passed by value-- another parameter. The following statement is true: objects are never passed in Java applications, only object references are passed. Therefore, objects are passed by reference. But it is important to distinguish how the parameters are passed, which is the intention of the excerpt. The fact that Java applications pass objects by reference does not mean that Java applications pass parameters by reference. Parameters can be object references, while Java applications pass object references by value.
Parameter passing in C++ and Java applications
Variables in a Java application can be one of two types: a reference type or a base type. When passed as a parameter to a method, the two types are handled in the same way. Both types are passed by value; neither is passed by reference. This is an important feature, as shown in subsequent code examples.
Before continuing the discussion, it is important to define the terms passing by value and by reference. Passing by value means that when a parameter is passed to a function, the function receives a copy of the original value. Therefore, if the function modifies the parameter, only the copy is changed, and the original value remains the same. Passing by reference means that when a parameter is passed to a function, the function receives the memory address of the original value, not a copy of the value. Therefore, if the function modifies the parameter, the original value in the calling code also changes.
Some confusion about parameter passing in Java applications stems from the fact that many programmers are moving from C++ programming to Java programming. C++ contains both non-reference types and reference types, passing them by value and by reference, respectively. The Java programming language has primitive types and object references; therefore, it is logical to think that Java applications, like C++, use passing by value for primitive types and passing by reference for references. After all, you would think that if a reference is being passed, it must be passed by reference. It's easy to believe this, and I actually believed it for a while, but it's not true.
In C++ and Java applications, when an argument passed to a function is not a reference, a copy of the value is passed (by value). The difference is in quotation. In C++, when the argument passed to the function is a reference, you pass this reference, or the memory address (by reference). In Java applications, when an object reference is a parameter passed to a method, you pass a copy of the reference (by value), not the reference itself. Notice that the object reference and copy of the calling method both point to the same object. This is an important difference. Java applications are no different from C++ in passing different types of parameters. The Java application passes all parameters by value, which makes copies of all parameters, regardless of their type.
Example
We will use the previous definition and discussion to analyze some examples. First consider a piece of C++ code. The C++ language uses both value-by-value and by-reference parameter passing mechanisms:
Listing 1 example of VRV Category +
# include#includevoid modify (int a, int* P, int & r); int main (int argc, char** argv) {int val, ref; int* pint; val = 10; ref = 50; pint = (int*) malloc (sizeof (int)); * pint = 15; printf ("val is% dn", val); printf ("pint is% dn", pint); printf ("pint is% dn", * pint); printf ("ref is% dnn", ref); printf ("calling modifyn") / / pass val and pint by value and ref by reference. Modify (val, pint, ref); printf ("returned from modifynn"); printf ("val is% dn", val); printf ("pint is% dn", pint); printf ("* pint is% dn", * pint); printf ("ref is% dn", ref); return 0;} void modify (int a, int * p, int & r) {printf ("in modify...n"); a = 0; * p = 7; p = 0; r = 0; printf ("an is% dn", a) Printf ("p is dn", p); printf ("r is dn", r);}
The output of this code is:
Listing 2 output from the code for the VRV Clover + code
Val is 10 pint is 4262128 * pint is 15 ref is 50 calling modify in modify... An is 0 p is 0 r is 0 returned from modify val is 10 pint is 4262128 * pint is 7 ref is 0
This code declares three variables: two integer variables and a pointer variable. The initial value of each variable is set and printed. At the same time, the pointer value and the value it points to are printed out. All three variables are then passed as arguments to the modify function. The first two parameters are passed by value, and the last parameter is passed by reference. The function prototype of the modify function indicates that the last parameter is passed as a reference. Recall that C++ passes all parameters by value, except references, which are passed by reference.
The modify function changes the values of all three parameters:
Set the first parameter to 0.
Set the value pointed to by the second parameter to 7, and then set the second parameter to 0.
Set the third parameter to 0.
The new value is printed out and the function returns. When the execution returns to main, the values of the three parameters and the values pointed to by the pointer are printed again. Variables passed as the first and second arguments are not affected by the modify function because they are passed by value. But the value pointed to by the pointer has changed. Note that, unlike the first two arguments, the variable passed as the last parameter is changed by the modify function because it is passed by reference.
Now consider similar code in the Java language:
Listing the 3:Java application
Class Test {public static void main (String args []) {int val; StringBuffer sb1, sb2; val = 10; sb1 = new StringBuffer ("apples"); sb2 = new StringBuffer ("pears"); System.out.println ("val is" + val); System.out.println ("sb1 is" + sb1); System.out.println ("sb2 is" + sb2); System.out.println ("); System.out.println (" calling modify "); / pass all parameters modify (val, sb1, sb2) by value System.out.println ("returned from modify"); System.out.println ("); System.out.println (" val is "+ val); System.out.println (" sb1 is "+ sb1); System.out.println (" sb2 is "+ sb2);} public static void modify (int a, StringBuffer R1, StringBuffer R2) {System.out.println (" in modify... "); a = 0; R1 = null; / / 1 r2.append (" taste good ") System.out.println ("an is" + a); System.out.println ("R1 is" + R1); System.out.println ("R2 is" + R2);}}
The output of this code is:
Output from the listing 4:Java application
Val is 10 sb1 is apples sb2 is pears calling modify in modify... An is 0 r1 is null r2 is pears taste good returned from modify val is 10 sb1 is apples sb2 is pears taste good
This code declares three variables: an integer variable and two object references. The initial values of each variable are set and printed. All three variables are then passed as parameters to the modify method.
The modify method changes the values of all three parameters:
Set the first parameter (integer) to 0.
Set the first object reference R1 to null.
Keep the value of the second reference R2, but change the object it references by calling the append method (this is similar to the handling of pointer p in the previous C++ example).
When the execution returns to main, print out the values of these three parameters again. As expected, the val of the integer has not changed. The object reference sb1 has not changed either. If sb1 is passed by reference, as many people claim, it will be null. However, because the Java programming language passes all parameters by value, a copy of the reference to sb1 is passed to the modify method. When the modify method sets R1 to null at / / 1, it does so only on a copy of the reference to sb1, not on the original value as it did in C++.
Also note that the second object reference sb2 prints out the new string set in the modify method. Even though the variable R2 in modify simply refers to a copy of sb2, they point to the same object. Therefore, the method called on the copied reference changes the same object.
Write an exchange method
Assuming we know how parameters are passed, writing an exchange function in C++ can be done in different ways. An exchange function that uses pointers is similar to the following code, where pointers are passed by value:
Listing 5: swap function using pointers
# include#includevoid swap (int * a, int * b); int main (int argc, char** argv) {int val1, val2; val1 = 10; val2 = 50; swap (& val1, & val2); return 0;} void swap (int * a, int * b) {int temp = * b; * b = * a; * a = temp;}
The swap function that uses references is similar to the following code, where references are passed by reference:
Listing 6: using referenced swap functions
# include#includevoid swap (int & a, int & b); int main (int argc, char** argv) {int val1, val2; val1 = 10; val2 = 50; swap (val1, val2); return 0;} void swap (int & a, int & b) {int temp = b; b = a; a = temp;}
Both C++ code examples exchange values as expected. If the Java application uses pass by reference, the following exchange method should work like the C++ example:
Whether the listing 7:Java exchange function passes parameters by reference as in C++
Class Swap {public static void main (String args []) {Integer a, b; a = new Integer (10); b = new Integer (50); System.out.println ("before swap..."); System.out.println ("an is" + a); System.out.println ("b is" + b); swap (a, b); System.out.println ("after swap..."); System.out.println ("an is" + a); System.out.println ("b is" + b) } public static void swap (Integer a, Integer b) {Integer temp = a; a = b; b = temp;}}
Because the Java application passes all parameters by value, this code does not work properly, and the resulting input is as follows:
Listing 8: output from listing 7
Before swap... An is 10 b is 50 after swap... An is 10 b is 50
So, how do you write a method in a Java application to exchange values of two basic types or two object references? Because the Java application passes all parameters by value, you cannot do this. To exchange values, you must do so inline outside the method call.
So much for sharing about the semantics of passing by value in Java applications. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.