When you pass an object reference as an argument to a method call what gets passed?
When you write your method, you determine the number and type of the arguments required by that method. You declare the type and name for each argument in the method signature. For example, the following is a method that computes the monthly payments for a home loan based on the amount of the loan, the interest rate, the length of the loan (the number of periods), and the future value of the loan (presumably the future value of the loan is zero because at the end of the loan, you've paid it off). Show
double computePayment(double loanAmt, double rate, double futureValue, int numPeriods) { double I, partial1, denominator, answer; I = rate / 100.0; partial1 = Math.pow((1 + I), (0.0 - numPeriods)); denominator = (1 - partial1) / I; answer = ((-1 * loanAmt) / denominator) - ((futureValue * partial1) / denominator); return answer; } double loanAmt double rate double futureValue int numPeriods type name As you can see from the body of the method, you simply use the argument name to refer to its value. Argument Types In Java, you can pass an argument of any valid Java data type into a method. This includes simple data types such as doubles, floats and integers as you saw in the Polygon polygonFrom(Point listOfPoints[]) { . . . }1 method above, and complex data types such as objects and arrays. Here's an example of a method that accepts an array as an argument. In this example, the method creates a new Polygon object from a list of Polygon polygonFrom(Point listOfPoints[]) { . . . }2, Polygon polygonFrom(Point listOfPoints[]) { . . . }3 points. Unlike some other languages, you cannot pass methods into Java methods.Polygon polygonFrom(Point listOfPoints[]) { . . . } Argument Names When you declare an argument to a Java method, you provide a name for that argument. This name is used within the method body to refer to the item. As with other names in Java, an argument name must be a legal Java identifier. A method argument can have the same name as one of the class's member variables. If this is the case, then the argument is said to hide the member variable. Arguments that hide member variables are often used in constructors to initialize a class. For example, take the following Circle class and its constructor: The Circle class has three member variablesclass Circle { int x, y, radius; public Circle(int x, int y, int radius) { . . . } } Polygon polygonFrom(Point listOfPoints[]) { . . . }2, y and Polygon polygonFrom(Point listOfPoints[]) { . . . }5. In addition, the constructor for the Circle class accepts three arguments each of which shares its name with the member variable for which the argument provides an initial value. The argument names hide the member variables. Thus using Polygon polygonFrom(Point listOfPoints[]) { . . . }2, Polygon polygonFrom(Point listOfPoints[]) { . . . }3 or Polygon polygonFrom(Point listOfPoints[]) { . . . }5 within the body of the constructor refers to the argument, not to the member variable. To access the member variable, you must reference it through Polygon polygonFrom(Point listOfPoints[]) { . . . }9--the current object: Names of method arguments cannot be the same as another argument name for the same method, the name of any variable local to the method, or the name of any parameter to aclass Circle { int x, y, radius; public Circle(int x, int y, int radius) { this.x = x; this.y = y; this.radius = radius; } } class Circle { int x, y, radius; public Circle(int x, int y, int radius) { . . . } }0 clause within the same method. Pass by Value In Java methods, arguments of simple data types are passed by value. When invoked, the method receives the value of the variable passed in, not a reference to the variable itself. For example, consider this series of Java statements which attempts to retrieve the current color of a Pen object in a graphics application:At the time when the. . . int r = -1, g = -1, b = -1; pen.getRGBColor(r, g, b); System.out.println("red = " + r + ", green = " + g + ", blue = " + b); . . .class Circle { int x, y, radius; public Circle(int x, int y, int radius) { . . . } }1 method is called, the variablesclass Circle { int x, y, radius; public Circle(int x, int y, int radius) { . . . } }2,class Circle { int x, y, radius; public Circle(int x, int y, int radius) { . . . } }3, andclass Circle { int x, y, radius; public Circle(int x, int y, int radius) { . . . } }4 all have the value -1. The caller is expecting theclass Circle { int x, y, radius; public Circle(int x, int y, int radius) { . . . } }1 method to pass back the red, green and blue values of the current color in theclass Circle { int x, y, radius; public Circle(int x, int y, int radius) { . . . } }2,class Circle { int x, y, radius; public Circle(int x, int y, int radius) { . . . } }3, andclass Circle { int x, y, radius; public Circle(int x, int y, int radius) { . . . } }4 variables. |