| title |
|---|
Methods |
- Break a problem into small manageable pieces.
- Code reuse
- Benefits: Saves time, money, security, simplification, organization
voidmethod: Performs a task and terminatesValue-returning method: Performs a task and sends a value back to the code that called it.
- To create a method you must write a definition
- Consist of a header and body
- e.g.,
- header:
public static void displayMessage() - body:
{ system.out.println(Hello"); }"
- header:
- e.g.,
- Consist of a header and body
Parts of a method header:
- Method modifiers:
- public: Method is available to code outside the class
- static: Method belong to a class, not a specific object.
- Return type
- Can be
voidor a data-type.
- Can be
- Method name
- Parenthesis
- Can contain nothing or a list of parameters
- A method executes when it is called.
- The
mainmethod is automatically called on program start- Other methods are executed by method call statements.
- Method modifiers and the void return type aren't written in the method call, they are only written in the method header.
- A method should always be documented by writing comments that appear before the method's definition
- The comments should provide a brief explanation of the method's purpose.
- Documentation comments begin with
/**and end with*/.- These comments can be read and processed by
javadocto produce HTML documentation.
- These comments can be read and processed by
Arguments: Values that are sent into a method.
- Data type of argument in a method call must match the variable declaration in the method header.
- Java will automatically perform widening conversions, but narrowing conversions will cause a compiler error (e.g., converting
doubletoint).
- Java will automatically perform widening conversions, but narrowing conversions will cause a compiler error (e.g., converting
- You can pass multiple arguments to a method (parameter list).
Parameter: Variable that holds the value being passed into a method.
- You may pass literals, the contents of variables, and the values of expressions as arguments
- Scope is the method in which it is declared. No statement outside the method can access the parameter variable by its name.
Passed by Value: Only a copy of an argument's value is passed into a parameter variable.
- All arguments of the primitive data types are passed by values.
When an object such as a String is passed as an argument, it is actually a reference to the object that is passed.
- Recall that a class type variable doesn't hold the actual data item, but the memory address of the object.
- Reference Variable: A variable associated with an object.
Immutable Objects: Objects that cannot be changed.
Strings are immutable objects.
@param tag: Lets you provide a description of each parameter in your documentation comments.
- All
@paramtags must appear after the general description, which can span several lines.
@return tag: Lets you provide a description of the return value.
- All
@returntags must appear after the general description, which can span several lines.
General Format:
@param parameterNameDescriptionLocal Variable: Variable declared inside a method.
- Is not accessible to statements outside the method.
- This is why different methods can have local variables with the same names.
- Exist only while the method is executing. Destroyed when method ends.
- Aren't automatically initialized with a default value, must be given a value before they can be used.
Data can be returned from a method to the statement that called it.
Example:
// Returns int value of 700 and assigned it to num
int num = Integer.parseInt("700");return statement: Causes method to end execution, returning a value to the statement hat called the method.
- Returned value must match the return type in the method header.
Examples:
- Returning a
booleanvalue
public static boolean isValid(int number)
{
if (number > 100)
return false;
return true;
}- Returning a Reference to a
StringObject
public static String fullname(String first, String last)
{
return first + " " + last;
}