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; } As you can see from the body of the method, you simply use the argument name to refer to its value. 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) { . . . } } x , y and radius . 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 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; } } catch() clause within
the same method. 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); . . . getRGBColor() method is called, the variables r , g , and
b all have the value -1. The caller is expecting the getRGBColor() method to pass back the red, green and blue values of the current color in the r , g , and b variables. However, the Java runtime passes the variables' values ( When control passes into the So,class Pen { int redValue, greenValue, blueValue; void getRGBColor(int red, int green, int blue) { . . . } } getRGBColor() gets access to the values of r , g , and b in the caller through its local variables red , green , and blue , respectively. The method gets a new copy of the values to use locally. Any changes made to those local variables are not reflected in the original variables from
the caller. Now, let's look at the implementation of This method will not work. When control gets to theclass Pen { int redValue, greenValue, blueValue; void getRGBColor(int red, int green, int blue) { red = redValue; green = greenValue; blue = blueValue; } } println() statement in this code snippet, getRGBColor() 's local variables red , green , and blue no longer exist. Therefore the assignments made to those variables had no effect; r , g , and b are all still equal to -1 . Passing variables by value affords the programmer some safety. Methods cannot unintentially modify a variable that is outside of its scope. However, you often want a method to be able to modify one or more of its arguments. The. . . int r = -1, g = -1, b = -1; pen.getRGBColor(r, g, b); System.out.println("red = " + r + ", green = " + g + ", blue = " + b); . . . getRGBColor() method is a case in point. The caller wants the method to return three values through its arguments. However, the method cannot modify its arguments, and, furthermore, a method can only return one value through its return value. So, how can a method return more
than one value, or have an effect (modify some value) outside of its scope. To allow a method to modify a argument, you must pass in an object. Objects in Java are also passed by value, however, the value of an object is a reference. So, the effect is that the object is passed in by reference. When passing an argument by reference, the method gets a reference to the object. A reference to an object is the address of the object in memory. Now, the local variable within the method is referring to the same memory location as the variable within the caller. So, let's rewrite the Now, we can rewriteclass RGBColor { public int red, green, blue; } getRGBColor() so that it accepts an RGBColor object as an argument. The getRGBColor() method returns the current color of the pen by setting the red , green and blue member variables of
its RGBColor argument. And finally, let's rewrite the calling sequence:class Pen { int redValue, greenValue, blueValue; void getRGBColor(RGBColor aColor) { aColor.red = redValue; aColor.green = greenValue; aColor.blue = blueValue; } } The modifications made to the RGBColor object within the. . . RGBColor penColor = new RGBColor(); pen.getRGBColor(penColor); System.out.println("red = " + penColor.red + ", green = " + penColor.green + ", blue = " + penColor.blue); . . . getRGBColor() method affect the object created in the calling sequence, because the names penColor (in the calling sequence) and aColor (in the getRGBColor() method) refer to the same object. The data type wrapper classes (Float, Integer, and so on) provided in the java.lang package are particularly useful for returning a single value whose type is one of Java's built-in simple types through a method's arguments. Under what circumstances can you return a reference from a method?Under what circumstances can you return a reference from a method? Only when it is safe to return, such as returning a reference from a method if the reference was passed into the method.
What type of statement causes a value to be sent back to the calling method?A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.
What is the return type of the readLine method call?1. The readLine() method of Console class in Java is used to read a single line of text from the console. Parameters: This method does not accept any parameter. Return value: This method returns the string containing the line that is read from the console.
When you declare an array What are the numeric fields initialized to?The default values of numeric array elements are set to zero, and reference elements are set to null . A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null . Arrays are zero indexed: an array with n elements is indexed from 0 to n-1 .
|