120 Chapter (Web site management) 5 Classroom Q & A Q:

120 Chapter 5 Classroom Q & A Q: What if I want the method to be able to change the contents of the argument? A: This cannot be done in Java. It s as simple as that. When using callby- value, changing the parameter in the method does not change the argument. Look closely at the setVolume() method of the Radio class discussed earlier. The last line of code in that method sets the parameter to 5. This does not change the argument to 5. We simply can t change the argument, even if we want to. Q: I suppose I can live with parameters not being able to change arguments, but what if I have a large amount of data that needs to be passed in to a method. I m talking very large. Typically, I would want to pass in a pointer so as to not waste the time and memory of copying large data. Can I avoid passing large objects in Java? A: Not only can you avoid passing large objects, you can t do it even if you want to. I need to reiterate a very important aspect of Java: A variable in Java is either one of the eight primitive data types or it is a reference. If the argument is a primitive data type, it is at most 64 bits in size (a double or a long). If the data I want to pass to a method is a very large object, it is not the object that is passed. It is a reference to the object, which is no larger than 64 bits, and in most cases is 32 bits. It is the reference that is copied, not the large amount of data. Q: So the largest amount of data that is copied with call-by-value in Java is only 64 bits? A: Correct! And copying 64 bits in today s computing world is rarely a concern in terms of performance or overhead. Q: What if I really want the method to change the argument passed in? A: Well, you just can t do it. But notice what you can do with the parameter if it is a reference to an object. The method can use this reference to do anything it wants to the object (depending on the access specifiers of the object s fields and methods). The method can change the data of the object being pointed to and invoke methods on the object. The only restriction arising from call-byvalue is that the method cannot change where the reference is pointing to.
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services

Leave a Reply