I first learned how to use java as a teaching material, that is, java programming ideology. I think this book is very detailed and thorough, but I do not understand some of the knowledge points, some think that these knowledge points are irrelevant to the main content. At that time, the project was tight, and the unintelligible knowledge had passed and programming was ready. After two years of java Development, I re-read this book to find the internal connection between these irrelevant knowledge. If I understood the intention of the author at that time, I would not take much detours in my work.
When I re-read chapter 12th, I thought that I had taken a lot of detours to solve a problem. In this chapter, I made a detailed discussion and explanation, took out the problem, and followed the detours I had taken, we will discuss each step and hope that beginners will take less detours.
Question 1: What should I do if I want a method to return two values when writing a program?
Discussion 1: Anyone who has used the C language knows that one of them can be returned through the return value, and the other can be returned through a pointer parameter (passing a pointer variable into the function and changing the content pointed to by the pointer, ). Java has no pointer. How can this problem be achieved? Chapter 12 makes it clear: "In fact, every object (except the basic number of drama types) in Java is a pointer. However, their use is strictly restricted and standardized ...". Can I use this to return multiple values? The answer is yes. The read method of the java. io. InputStream class is used in this way. For more information, see public int read (byte [] B ).
Tip 1: There are two parameter passing methods for the java method: 1. Pass by value: This method is used for the basic data type of java; 2. Pass by reference, other types of objects are passed references ). There is a problem with the method of transferring references, that is, two references point to the same object. An object is modified in the function, and the object outside the function is also modified, this can easily cause side effects of aliases.
Conclusion 1: In some cases, the side effects of aliases can be used to return multiple return values. For example, the read method of the java. io. InputStream class.
Question 2: According to conclusion 1, some custom classes can generally be used as output parameters to return values. But how can we solve the problem of basic data types?
Discussion 2: Since each basic data type corresponds to a packaging class, can the corresponding packaging class be used for the purpose? Practice has found that the objects of these packaging classes cannot be modified, and they are all read-only objects. Once created, the objects of these classes cannot be modified, and the packaging class cannot be used for the purpose. In this case, it is found that an array can be used for the purpose. You can create an array of basic types of data and pass it as a parameter to the method to change the array content in the method.
Tip 2: some classes are read-only. Once these classes are created, they cannot be modified. The packaging classes of basic data types are read-only classes. These classes actually use the unchanged mode mentioned in java and patterns. Advantages of the constant mode: 1. unchanged objects are easier to maintain than mutable objects; 2. thread security. Disadvantage: You must create a new object once you need to modify an unchanged object. Some discussions about the differences between String and StringBuffer are nothing more than the constant mode of String, but the constant mode of StringBuffer, which is essentially a discussion of the constant mode.
Conclusion 2: an array of basic data types can be used as an output parameter of the method, but the packaging class of basic data types cannot be used. (To use a basic data type as an output parameter, create an array. It is only a method to achieve the goal and is not recommended .)
Question 3: Application conclusion 2: when calling RMI, the output parameter changes while calling the method, but does not change at the call. Why?
Discussion 3: When I write an rmi service interface, use the following method interface: byte [] getFaxByte (Fax fax, int [] page), where page is used as the output parameter, you want the function to change the value of page [0] During the call. After the function is called, the modified value is obtained by referencing the page. In practice, I found that the rmi service and call on the same machine can get the correct result, and the rmi service and call are not on the same machine, and the result is incorrect.
After one-step tracking, I found the root cause of the problem. I was not familiar with the rmi principle and did not understand the foundation of rmi.
Tip 3: rmi is a remote method call, essentially to allow one JVM to call another JVM method. The basic principle of Rmi is to use stub and skel to disguise remote objects as a local object in your own machine. It is easy to ignore: the parameters and return parameters of the remote interface must implement the Serializable interface. During remote calls, the parameters are uploaded to the remote server through Serializable, the remote return result Serializable is passed back to the called machine. In this case, because the parameter in the method call is not the same object as the Method Processing reference, there is no side effect of the alias mentioned in conclusion 1. in rmi call, using output parameters will not succeed.
Conclusion 3: output parameters cannot be used in rmi and return values.
Question 4: How to return multiple parameters in rmi?
Discussion 4: I first thought of Collection. Collection can accommodate multiple objects and define the return value as Collection. It can include multiple objects in the Collection as return values, in this way, multiple parameters can be returned.
Conclusion 4: Collection can be used to return multiple variables in rmi calls.
Problem 5: The Collection only contains objects of the Object type, and the Object information is lost. In addition, function definitions and function calls must provide a good description of the order of objects contained in the Collection. That is to say, the coupling between a function and a call is too strong, and the reusability of such a function is too low. Function users need to know this function very well and are prone to errors.
Discussion 5: Conclusion 4: the idea of programming is still an object-oriented idea. After reading the design model and reconstruction books, I feel that my thinking is still somewhat problematic. If a method needs to return more than two return values, these return values are usually correlated and correlated, a new class should be created to include the attribute to be returned in the class, use the newly created class as the return value.
Conclusion 4: In the process of solving the problem, we inadvertently compared the differences between process-oriented and object-oriented ideas for a simple problem. We have some experience on one aspect of OOP, it helps to transform personal programming ideas from process-oriented to OOP.
Conclusion 5: If the function needs to return multiple return values, try to create a new class to solve the problem.
Question 6: If a function requires multiple return values and each return value is not associated with each other, how can this problem be solved?
Discussion 6: will this happen? The refactoring book has a point of view that a method should complete only one function. When Problem 6 occurs, should we check whether there is a problem with the method definition? Then, check whether the class definition is correct? Check whether code reconstruction is required?
When I first started using java, I often encountered the problem of returning multiple return values. After reading the relevant information about heavy construction and design patterns, I can solve the problem by using conclusion 5, the possibility of Problem 6 is basically absent. Although Question 6 can still be solved by conclusion 4, I think from the perspective of OOP, we should check whether our class design is reasonable.
Harvest 5: I realized the benefits of refactoring to improve the code quality. I will try refactoring later.
Conclusion 6: During the coding process, we found some coding problems that are difficult to solve with our own knowledge. We can use another idea to check whether the initial design idea is correct and use refactoring to improve the code quality.
Reprinted please indicate from http://www.csdn.net/develop/author/netauthor/xuyongshuo/