Chapter 5 – Methods – Flashcards
Unlock all answers in this set
Unlock answersquestion
Methods can be used to break a complex program into small, manageable pieces. A method is a collection of statements that performs a specific task.
answer
Introduction to Methods
question
Simply executes a group of statements and then terminates. Example: System.out.println
answer
Void method
question
Returns a value to the statement that called it. Example: The Random class's nextInt method number = rand.nextInt();
answer
Value-Returning method
question
Writing the code to perform a task once and then reusing it each time you need to perform the task.
answer
Code Reuse
question
Consists of two general parts: a header and a body. Method Header: Appears at the beginning of a method definition, lists several important things about the method, including the method's name. Method Body: A collection of statements that are performed when the method is executed. These statements are enclosed inside a set of curly braces. *Example* public static void displayMessage () { Systmem.out.println("Hello from the displayMessage method."); }
answer
Method Defintion
question
The key words public and static are modifiers. *Public* - means that the method is publicly available to code outside the class. *Static* - means that the method belongs to the class, not a specific object.
answer
Method Modifiers
question
When the keyword *void* appears here, it means that the method is a void method, and does not return a value.
answer
Return Type
question
You should give each method a descriptive name. In general, the same rules that apply to variable names also apply to method names. This method is named *displayMessage* so we can easily guess what the method does: It displays a message.
answer
Method Name
question
In the header, the method name is always followed by a set of parenthesis. As you will learn later in this chapter, methods can be capable of receiving arguments. When this is the case, a list of one or more variable declarations will appear inside the parentheses. The method in this example does not receive any arguments, so the parentheses is empty.
answer
Parentheses
question
A method executes when it is called. The main method is automatically called when a program starts, but other methods are executed by method call statements. Example: displayMessage(); The statement is simply the name of the method followed by a set of parentheses.`
answer
Calling a method
question
In other words, method A can call method B, which can then call method C. When method C finishes, the JVM returns to method B. When method B finishes, the JVM returns to method A.
answer
Hierarchical Method Calls
question
You should always document a method by writing comments that appear just before the method's definition. The comments should provide a brief explanation of the method's purpose.
answer
Using Documentation Comments w/Methods
question
A method may be written so it accepts arguments. Data can then be passed into the method when it is called.
answer
Passing Arguments to a Method
question
Values that are sent into a method. *Examples* System.out.println("Hello"); - This statement calls the System.out.println method and passes "Hello" as an argument. number = Integer.parseInt(str); - This statement calls the Integer.parseInt method and passes the contents of the str variable as an argument. By using parameter variables, yo can design your own methods that accept data this way.
answer
Arguments
question
Sometimes simply referred to as a parameter, is a special variable that holds a value being passed into a method. Here is the definition of a method that uses a parameter: public static void displayValue(int num) { System.out.println("The value is " + num); } - (int num), This is the declaration of a parameter variable, which enables the displayValue method to accept an integer value as an argument. *Example of a call to displayValue method* displayValue(5); - This statement executes the displayValue method. The argument that is listed inside the parentheses is copied into the method's parameter variable, num.
answer
Parameter Variable
question
When you pass an argument to a method, be sure that the argument's data type is compatible with the parameter variable's data type. Java will automatically perform a widening conversion if the argument's data type is ranked lower than the parameter variable's data type. For example, the displayValue method has an int parameter variable. Both short and byte arguments are automatically converted to an int. HOWEVER, Java will not automatically convert an argument to a lower-ranking data type. This means that a long, float, or double value cannot be passed to a method that has an int parameter variable.
answer
Argument and Parameter Compatibility
question
Recall from Chapter 2 that a variable's scope is the part of the program where the variable may be accessed by its name. A variable is visible only to statements inside the variable's scope. A parameter variable's scope is the method in which the parameter is declared. No statement outside the method can access the parameter variable by its name.
answer
Parameter Variable Scope
question
*Example* public static void showSum(double num1, double num2) { double sum; sum = num1 + num2; System.out.println("The sum is " + sum); } - Notice that two parameter variables, num1 and num2, are declared inside the parentheses in the method header. This is often referred to as a parameter list. Also notice that a comma separates the declarations. Here is an example of a statement that calls the method: showSum(5,10) * Warning * Each parameter variable in a parameter list must have a data type listed before its name! public static void showSum(*double* num1, *double* num2)
answer
Passing Multiple Arguments
question
In Java, all arguments of the primitive data types are passed by value, which means that only a copy of an argument's value is passed into a parameter variable. Look at Code Listing 5-6 Even though the parameter variable myValue is changed in the changeMe method, the argument number is not modified. The myValue variable contains only a copy of the number variable and that is what is changed.
answer
Passed By Value
question
You can also write methods that accept references to objects as arguments. * For Example * public static void showLength(String str) { System.out.println(str + " is " + str.Length() + " characters long."); } - This method accepts a String object reference as its argument, and displays a message showing the number of characters in the object. Calling the method: String name = "Warren"; showLength(name); - When this code executes, it will display: Warren is 6 characters long.
answer
Passing Object References to a Method
question
In Java, String objects are immutable, which means they cannot be changed. Look at Code Listing 5-7
answer
Immutable
question
When writing the documentation comments for a method, you can provide a description of each parameter by using a @param tag. When the javadoc utility sees a @param tag inside a method's documentation comments, it knows that the documentation for a parameter variable appears next. General Format: @param parameterName Description * All @param tags must appear after the general description of a method. * Description can span several lines and ends at the end of comment (*/) or at the beginning of another tag
answer
Using the @param Tag in Documentation Comments
question
A local variable is declared inside a method and is not accessible to statements outside the method. Different methods can have local variables with the same names because the methods cannot see each other's local variables. The program can see only one at a time because they are in different methods.
answer
More about Local Variables
question
A method's local variables exist only while the method is executing. This is known as the lifetime of a local variable.
answer
Local Variable Lifetime
question
It is possible to use a parameter variable to initialize a local variable. Example: public static void showSum(double num1, double num2) { *double* sum = num1 + num2; System.out.println("The sum is " + sum); }
answer
Initializing Local Variables with Parameter Values
question
A method may send a value back to the statement that called the method. Methods that return a value are appropriately known as value-returning methods. Examples: Integer.parseInt() & Math.pow()
answer
Returning a Value from a Method
question
You must decide what type of value the method will return. This is because you must specify the data type of the return value in the method header. Recall that a void method, which does not return a value, uses the key word void as its return type in the method header. A value-returning method will use int, double, boolean, or any other valid data type in its header. *Example* public static *int* sum(int num1, int num2) { int result; result = num1 + num2; return result; } - Return type is int - "return result;" is a return statement. You must have a return statement in a value-returning method. It causes the method to end execution and it returns a value to the statement that called the method. - General Format: return Expression; Expression can be any variable, literal, or mathematical expression. we could of said "return num1 + num2;" instead of "return result;"
answer
Defining a Value-Returning Method