In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to correctly use the input parameter to do the return value of the Java trap". The explanation in the article is simple and clear and easy to learn and understand. please follow the editor's train of thought to study and learn "how to correctly use the input parameter to do the return value of the Java trap".
Problem background
For example, there is a piece of code:
@ Namedpublic class AService {private SupplyAssignment localSupply = new SupplyAssignment (); @ Inject private BService bervice; public List calcSupplyAssignment () List supplyList = bService.getLocalSupplyList (this.localSupply); … Return supplyList;}}
In the above code, Service A wants to call Service B to obtain supplyList, but at the same time, Service An also wants to modify the status value of localSupply, which can not avoid modifying the calcSupplyAssignment interface (do not want to change the returned type), using localSupply as the input parameter but also as the return value.
The service B code is as follows:
@ Namedpublic class BService {public List getLocalSupplyList (SupplyAssignment localSupply) SupplyAssignment supplyAssignment = this.getSupplyAssignment (); / / I hope localSupply will return localSupply = supplyAssignment; after being reassigned. Return supplyList;}}
Within the service B code, the input parameter localSupply of service An is passed in, hoping to be re-assigned by supplyAssignment and return a new value. However, this is ineffective.
Cause of the problem
Let's first take a look at the types of parameter passing in the programming language:
Value transfer (pass by value) means that a copy of the actual parameters is passed to the function when the function is called, so that if the parameters are modified in the function, the actual parameters will not be affected.
Reference passing (pass by reference) means that the address of the actual parameter is passed directly to the function when the function is called, so the modification of the parameter in the function will affect the actual parameter.
Because the Java programming language uses value passing, because Java does not have the concept of pointers. That is, the method gets a copy of all the parameter values, and the method cannot modify the contents of any parameter variables passed to it.
Therefore, in the above code, when Service An invokes Service B, the parameter localSupply of Service B is actually a copy of Service A's localSupply, both of which, of course, point to the same address object supplyAssignment1.
When the parameter localSupply is re-assigned to service B as localSupply = supplyAssignment, in fact, only the parameter localSupply of B is re-assigned, and the parameter localSupply of B points to a new address object supplyAssignment2.
You can see clearly from the figure above that localSupply of service An and parameter localSupply of service B already point to different objects, and any modification to the parameter localSupply of B will not affect the original value of localSupply of service A. This is the reason for the problem. You want Service B to modify the status of the input parameter of Service An and return the changed value to Service A, but it doesn't work.
Solution 1: input parameters should not be used as return values
Of course, this is the clearest and easiest to understand, but it can lead to a change in the return type of some interfaces.
Sometimes you do want to enter the parameter to do the return value, then look at option 2.
Option 2: input parameters do not assign new objects
The solution is to make state changes directly on the objects that enter the parameters, rather than assigning new objects. It's the same picture:
In this diagram, as long as we have been in the parameter localSupply of B to modify the state value of supplyAssignment1, the result can be fed back to the localSupply of Service A. How to achieve it? Look at the following code:
@ Namedpublic class BService {public List getLocalSupplyList (SupplyAssignment localSupply) SupplyAssignment supplyAssignment = this.getSupplyAssignment (); / / you cannot create a new reference for localSupply, you can only reassign the attribute BeanUtils.copyProperties (supplyAssignment, localSupply);... Return supplyList;}}
In the above method, we use Spring's utility class BeanUtils. The essence of the copyProperties method of this class is to assign the property value of supplyAssignment to the property of localSupply. This means that we are modifying the properties on the parameter localSupply of B, and not creating a new object.
Thank you for your reading, the above is the content of "how to correctly use the input parameter as the return value of the Java trap". After the study of this article, I believe you have a deeper understanding of how to correctly use the input parameter of the Java trap as the return value, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.