Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Whether the Java language passes values or references

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you whether the Java language is passed or quoted, I believe most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it together.

1. Simple types are passed by value

When the parameters of the Java method are simple types, they are passed by value (pass by value). This can be illustrated by a simple example:

/ * example 1 * / / * * @ (#) Test.java * @ author fancy * / public class Test {public static void test (boolean test) {test =! Test; System.out.println ("In test (boolean): test =" + test);} public static void main (String [] args) {boolean test = true; System.out.println ("Before test (boolean): test =" + test); test (test); System.out.println ("After test (boolean): test =" + test);}}

Running result:

Before test (boolean): test = true

In test (boolean): test = false

After test (boolean): test = true

It is not difficult to see that although the value of the passed parameter is changed in the test (boolean) method, it has no effect on the parameter source variable itself, that is, it has no effect on the test variable in the main (String []) method. That means that when the parameter type is a simple type, it is passed by value. When a simple type of variable is passed in the form of a parameter, the value of the parameter is actually passed into the method function, so no matter how to change its value in the method function, the result is only the copied value, not the source value.

two。 What is a citation?

The main problem with whether Java passes a value or a reference lies in the passing of objects, because simple types in Java have no references. Now that citation is mentioned in the argument, in order to figure out this problem, we must know what the citation is.

To put it simply, a reference is actually like an object's name or alias. An object requests a piece of space in memory to hold data, and it may take up varying amounts of space depending on the size of the object. When we access an object, we do not directly access the data of the object in memory, but through references. A reference is also a data type, and we can think of it as something similar to a pointer in C that indicates the address of the object in memory-- but we can't observe what that address is.

If we define more than one reference to the same object, then these references are different because the reference is also a data type and requires a certain amount of memory space to save. But their values are the same, indicating the location of the same object in memory. such as

String a = "Hello"

String b = a

Here, an and b are two different references, and we use two definition statements to define them. But their values are the same, pointing to the same object "Hello". You may also find it not intuitive enough, because the value of the String object itself is immutable (like b = "World"; b = a; instead of changing the value of the "World" object, it changes the value of its reference b to point to another String object a). So let's use StringBuffer to give an example:

/ * example 2 * / / * @ (#) Test.java * @ author fancy * / public class Test {public static void main (String [] args) {StringBuffer a = new StringBuffer ("Hello"); StringBuffer b = a; b.append (", World"); System.out.println ("an is" + a);}}

Running result:

An is Hello, World

In this example, both an and b are references, and when the value of the object indicated by b is changed, the value of the object indicated by an also changes from the output. So, both an and b point to the same object, that is, a StringBuffer object that contains "Hello".

Here I describe two main points:

1. A reference is a data type that holds the address of an object in memory. This type is neither a simple data type nor a class instance (object).

two。 Different references may point to the same object, in other words, an object can have multiple references, that is, variables of this type.

3. How is the object passed?

There are two ways of saying about the passing of objects, namely, "it is passed by value" and "it is passed by reference". Each of these two statements has its own truth, but neither of them is analyzed in essence, that is, there is a dispute.

Now that we know what a reference is, let's take a look at how object parameters are passed. Let's take a program as an example:

/ * example 3 * / / * * @ (#) Test.java * @ author fancy * / public class Test {public static void test (StringBuffer str) {str.append (", World!");} public static void main (String [] args) {StringBuffer string = new StringBuffer ("Hello"); test (string); System.out.println (string);}}

Running result:

Hello, World!

Test (string) calls the test (StringBuffer) method, passing string as an argument. There is no doubt that string is a reference here. As mentioned earlier, a reference is a data type, and it is not an object, so it cannot be passed by reference, so it is passed by value, what is its value? Is the address of the object.

So you can see that objects are passed by value when they are used as parameters, right? Wrong! Why is it wrong? let's look at another example:

/ * example 4 * / / * @ (#) Test.java * @ author fancy * / public class Test {public static void test (String str) {str = "World";} public static void main (String [] args) {String string = "Hello"; test (string); System.out.println (string);}}

Running result:

Hello

What causes it? Because the parameter str is a reference, and it is a different reference from string, although they are both references to the same object. Str = "World" changes the value of str to point to another object, but the object that str points to changes, but it does not have any effect on "Hello", and because string and str are different references, the change of str does not have any effect on string, as shown in the example.

The result is to overturn the argument that parameters are passed by value. So when an object is used as a parameter, it is passed by reference? And wrong! Because the previous example does show that it is passed by value.

As a result, just like the question of whether light is a wave or a particle, the answer to the question of how to pass the parameters of the Java method can only be: either by value or by reference, but the result will be different if the reference is different.

4. Take a correct look at the question of passing value or citation

In order to look at this problem correctly, we must find out why there is such a problem.

In fact, the problem comes from C, not Java.

There is a data type called pointer in C language, so when you pass a data as an argument to a function, there are two ways: pass a value or pass a pointer. The difference between them can be illustrated by a simple example:

/ * example 5 * / / * @ (#) test.c * @ author fancy * / void SwapValue (int a, int b) {int t = a; a = b; b = t;} void SwapPointer (int * a, int * b) {int t = * a; * a = * b; * b = t;} void main () {int a = 0, b = 1; printf ("1: a =% d, b =% dn", a, b); SwapValue (a, b) Printf ("2: a =% d, b =% dn", a, b); SwapPointer (& a, & b); printf ("3: a =% d, b =% dn", a, b);}

Running result:

1: a = 0, b = 1

2: a = 0, b = 1

3: a = 1, b = 0

You can clearly see that passing parameters by pointer can easily modify the values passed in by parameters, but not by value.

When Java grew up, many C programmers turned to Java and found that using a method like SwapValue still could not change the value of a simple data type passed in by parameters, but if it was an object, its members might be changed at will. So they think it's like the problem of passing values / pointers in C language. But there are no pointers in Java, so the problem becomes a problem of passing values / references. Unfortunately, it is not appropriate to discuss this issue in Java.

The ultimate purpose of discussing such a problem is to figure out when it is convenient to change the value of a parameter in a method function and make it valid for a long time.

In Java, there are two cases of changing the value of a parameter, the first is to use the assignment symbol "=" to directly assign the value to make it change, such as example 1 and example 4, and the second is to change the member data of some objects through certain ways, such as example 3. In the first case, the change does not affect the data outside the method, or directly the source data. The second method, on the other hand, affects the source data-- because the object indicated by the reference has not changed, a change to its member data is essentially a change to that object.

5. How to implement a method similar to swap

The problem of passing a value or passing a reference is solved, but we still can't solve the problem: what if I have two int variables an and b and I want to write a method to exchange their values?

The conclusion is very disappointing-there is no way! Therefore, we can only discuss it on a case-by-case basis, taking the sorting that often uses the exchange method as an example:

/ * * example 6 * / / * @ (#) Test.java * @ author fancy * / public class Test {public static void swap (int [] data, int a, int b) {int t = data [a]; data [a] = data [b]; data [b] = t;} public static void main (String [] args) {int [] data = new int [10]; for (int I = 0; I

< 10; i++) { data[i] = (int) (Math.random() * 100); System.out.print(" " + data[i]); } System.out.println(); for (int i = 0; i < 9; i++) { for (int j = i; j < 10; j++) { if (data[i] >

Data [j]) {swap (data, I, j);} for (int I = 0; I < 10; iTunes +) {System.out.print ("" + data [I]);} System.out.println ();}}

Run result (one of the cases):

78 69 94 38 95 31 50 97 84 1

1 31 38 50 69 78 84 94 95 97

The swap (int [] data, int a, int b) method internally actually changes the member data of the object indicated by data, that is, the second method discussed above to change the parameter value. I hope you can draw an example and use similar methods to solve related problems.

The above is all the contents of the article "whether the Java language is passed or quoted". Thank you for your reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report