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

It is true that only values are passed in Java

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail about Java really only value transfer, the quality of the article content is high, so Xiaobian share for everyone to make a reference, I hope you have a certain understanding of related knowledge after reading this article.

Review value passing and reference passing

There are different opinions on the web about whether Java is value passing or reference passing.

Basic types or wrapper classes of basic types and String are value passing, and reference types are reference passing. In Java, only values are passed.

This issue should be controversial. Based on the test results and our own experience, as well as word of mouth or school teachers, we think it is the first kind. But the second argument is also very popular, and gradually we also think that the second is correct. So let's analyze this problem below.

Before we talk about this, let's first understand the concepts and phenomena of value passing and reference passing. I still remember that value passing and reference passing were concepts that my teacher taught me when I was studying Java in college. What are their concepts? The teacher explained it through examples, probably like this.

value is passed

Example 1:

public static void main(String[] args){ TestJavaParamPass() tjpp = new TestJavaParamPass(); int num = 10; tjpp.change(num); System.out.println("num in main():"+num); } public void change(int param){ param = 20; System.out.println("param in change():"+param);}

Console output:

param in change():20num in main():10

The int variable num in the mian() method is passed to the change() method, which changes the value to 20. By looking at the console output, the num variable value in the main() method has not changed.

Conclusion: The basic type is value transfer.

reference passing

Example 2:

public static void main(String[] args){ TestJavaParamPass() tjpp = new TestJavaParamPass(); User user = new User(); user.setName("Jerry"); tjpp.change(user); System.out.println("user in mian():"+user); } public void change(User param){ param.setName("Tom"); System.out.println("param in change():"+param);}

Console Output:

param in change():User(name=Tom}user in mian():User(name=Tom}

The user variable in the main() method is passed to the change() method, which changes the value of its name attribute. By looking at the console output, the name attribute value of the user variable in the main() method changes.

Conclusion: Reference types are reference-transitive.

Special value transfer

Example 3:

public static void main(String[] args){ TestJavaParamPass() tjpp = new TestJavaParamPass(); String name = "Jerry"; tjpp.change(name); System.out.println("name in main():"+name); } public void change(String param){ param = "Tom"; System.out.println("param in change():"+param);}

Console Output:

param in change():Tomname in mian():Jerry

String is also a data type of reference type, why hasn't the value changed? Because param = "Tom"; in the change() method is equivalent to param = new String("Tom"); is equivalent to param being reassigned to point to another object. So, in fact, String type is passed as a reference, but it has been reassigned to point to another object, without modifying the original object. That is, String is essentially reference passing, and the table is value passing.

Conclusion: Basic type is value passing, reference type is reference passing, String is special value passing.

This conclusion is also spread on the network more, probably most programmers 'cognition is like this. As for the concepts of value passing and reference passing, the following can be inferred from the above examples and conclusions, and explain why most programmers understand String as a special value passing.

concept extraction

It's better to call it conclusion than concept extraction.

Value passing: When a variable of a primitive type is passed to a method, it is the value of that variable that is passed (i.e., copies its own value to the method).

Reference passing: When a variable of a reference type is passed to a method, it passes a reference to the variable (i.e., the memory address to which it points).

Why String is a special value transfer: Because String and the basic type show the same result from the representation, probably to facilitate the memory of this result. However, you should know that String is also passed by reference, but its reference has been reassigned to point to another object, so it will not affect the original value. So String cannot simply be said to be value passing.

Resolving Java's statement that only value is passed

only value passing.

There is also a saying circulating on the Internet called Java only value passing. There are articles on the Internet that demonstrate Java's value-only argument, with examples similar to those above.

The analysis is thorough and explains the essence of the above three examples. Point out the error of the second example above, the example given is inappropriate. And pointed out that the following example is appropriate, and gave the key and the house example, to prove that the second example above is indeed inappropriate. Because the above examples focus on whether the value of the final actual variable has changed.

public static void main(String[] args){ TestJavaParamPass() tjpp = new TestJavaParamPass(); User user = new User(); user.setName("Jerry"); tjpp.change(user); System.out.println("user in mian():"+user); } public void change(User param){ param = new User() param.setName("Tom"); System.out.println("param in change():"+param);}

Output:

param in change():User(name=Tom}user in mian():User(name=Jerry}

The conclusion of the last article is that Java has only value passing. Reference types are roughly interpreted as follows (basic types aside): the actual variables (actual parameters) are assigned a value of their own reference address and passed to the method, and the formal parameters of the method get the value of the reference address of the actual parameter. The emphasis is on values, so the conclusion is that reference types are also value transfers.

parsing

I think it is not a problem for the arguer to analyze the principle of the variation of the actual parameters of the basic type and the reference type, but the conclusion is not a bit inappropriate. What can I say? Please continue watching.

Argument means java only passes values. That is to say, the reference type is also a value transfer, the emphasis is to copy the value of a reference address to the formal parameter, because the value here is the value of the reference address (not the value stored in the memory pointed to by the reference), so it is a value transfer. Isn't that a little farfetched? I think it's a bit of a steal, yes, we all know that reference types pass reference values, but you can't say that just because you pass values, what else can you pass without passing values? A reference is a memory address, so shouldn't it also be expressed as a value?

Traditionally, the basic type is value passing (the value represented by something stored in memory), and the reference type is reference passing. I think the emphasis is on: basic types pass a copy of the value, reference types pass a copy of the reference. The emphasis is on what is passed. What is passed by the basic type is called the value of the variable (the value represented by the variable itself), and what is passed by the reference type is called the reference (the value of the reference itself, that is, the memory address), not the value in the memory space to which the reference points. So it's okay to understand that reference types pass references.

Therefore, in Java, the basic type passes the value represented by the variable itself (the value represented by the stored thing in memory), which is value passing; the reference type passes the reference of the object, which is reference passing; further, the reference is also an exact value to represent, or you regard the reference as the value of the object, which can also be said that the reference type passes the value of the object, which is value passing.

The article also said

Whether it is value passing or reference passing, it is actually an evaluation strategy. In the evaluation strategy, there is also a call by sharing. In fact, in Java, parameter passing should be strictly shared.

Passed by share means that when a function is called, what is passed to the function is a copy of the address of the argument (or directly if the argument is on the stack). When operating on parameters inside a function, you need to copy the address to find the specific value before operating. If the value is on the stack, then because it is a direct copy of the value, the function's internal manipulation of the parameter has no effect on the external variable. If the original copy is the address of the original value in the heap, then you need to find the corresponding position in the heap according to the address, and then perform the operation. Operations on values within functions are visible to external variables because they are copies of addresses passed.

To put it simply, passing in Java is value passing, and this value is actually a reference to the object.

The meaning here is that whether it is a basic type or a reference type, what is passed to the function is the address copy of the argument, that is, the memory address, which can be said to be a reference, but the basic type is in the stack, the value copied directly when the parameter is operated in the function, and the value of the reference type is in the heap. You need to find its location first, that is, the address and reference. Finally, java is value passing, and this value is a reference to the object.

See this?

Address is a reference, so can we say java is passed by reference? What is passed is the value of the reference. Isn't it all the value in the computer? What else can it be if it isn't the value? It's said that it's a reference. The transfer is different in emphasis. What's passed is the address is the reference. The reference does not use the value to indicate what to use.

The value mentioned here is not a concept. It is said that the basic type passes the value. This is the value of the value variable itself. It is also said that the object passes the value. This value is said to be the address of the reference, while the object is said to be passed by reference. The emphasis lies in the address of the transmission, pointing to the address of the internal attribute represented by the object, and the value of the internal attribute represented by the non-object. The purpose is to distinguish it from the basic type directly passing the value.

Wikipedia: References (programming)

In computer science, a reference is a value that allows a program to indirectly access a particular piece of data in computer memory or other storage devices, either as a variable or as a record. References are different from the data itself. In general, a reference would be the physical address at which data is stored in memory or storage. Therefore, references are often referred to as pointers or addresses to the data.

Looking at the definition of a reference, a reference refers to a value of XXX data. Well, the reference itself is a value. But what else could it be? Isn't everything a value in a computer?

There is nothing wrong with saying value passing or reference passing. The key is how you define and explain value passing, reference passing, and what value represents.

Everything in the computer has values, and if you start from this point, it's all passed values, but refined to java, basic types pass their own values, reference types pass the values of references, not the values of attributes within objects.

So if arbitrarily said that only the value transfer is also no problem, because in the computer can only use the value to express ah, but feel a little opportunistic, just like saying that there is only ** in the world, then still distinguish ** and ** why, the reason is similar. (I can't think of a good example yet.)

The key point is that what is passed is the address of the reference, rather than the internal attribute value represented by the object. It is distinguished from the basic type by passing the value directly, which is easy to remember.

We can understand the principle of phenomenon, there is no need to chase so deep, or play word games, drill horns.

If someone asks you, you can say that basic types and their wrapper classes are value passing, reference types pass references to objects that are address values, String passes address values, but the address values are modified within the function, so it does not affect the arguments, because it behaves the same as basic types, so it may be convenient to remember that String is value passing. In the end, it's all about values, but values have different meanings.

About Java really only value transfer to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

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