What are methods?
What are the parts of a method header:
public static void displayMessage()
public static: Method Modifiers
void: Return Type
displayMessage: Method Name
New method has to be in the class definition NOT the main method – has a method header, parentheses beside method name and its own open and close curly braces to encapsulate code
What does displayMessage(); mean?
The JVM branches to the “displayMessage()” method and performs the statement in its body.
Where can method call statements be used?
Method call statements may be used in control structures like loops, if statements, and switch statements.
How else can methods be called?
Methods can also be called in a hierarchical, or layered fashion. 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.
What is an argument?
System.out.println("Hello");This statement calls the System.out.println method and passes “Hello” as an argument. Here is another example:
number = Integer.parseInt(str);
This statement calls the Integer.parselnt method and passes the contents of the str variable as an argument. By using “parameter variables”, you can design your own methods that accept data this way. See the displayValue
method.
displayValue(5);
This line is an example of a call to the displayValue method, passing 5 as an argument
What else can you pass as the contents in the argument?
You may also pass the contents of variables and the values of expressions as arguments, For example, the following statements call the displayValue method with various arguments passed:
// int x = 10;
// displayValue(x);
// displayValue(x * 4);
// displayValue(Integer.parseInt(“700”));
What is a parameter variable?
A “parameter variable”, sometimes simply referred to as a parameter, is a special variable that holds a value being passed into a method.
public static void displayValue(int num)
{
System.out.println(“The value is “ + num);
Notice the integer variable declaration that appears inside the parentheses (int num).This is the declaration of a “parameter variable”, which enables the displayValue method to accept an integer value as an argument.
What does Java do if the argument’s data type is ranked lower than the parameter varaible’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.
What do you do to convert an argument to a lower-ranking data type?
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. You can use a cast operator to convert a value manually to a lower-ranking data type.
How to pass more than one agrument to a method?
double value1 = 2.5;
double value2 = 3.5;
showSum(value1, value2);
When the showSum methods executes as a result of this code, the num parameter will contain 2.5 and the num2 parameter will contain 3.5.
What is a parameter list?
public static void showSum(double num1, double num2)
{
double sum; // To hold the 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. See the “showSum” call statement in the main method.
What happens when a string is passed as an argument?
What is a local variable?
What does this code mean?
public static void main(String[] args)
{
texas();
california();
}
public static void texas()
{
int birds = 5000;
System.out.println("In texas there are " + birds + " birds.");
}
public static void california()
{
int birds = 3500;
System.out.println("In California there are " + birds + " birds.");
}How does a method send a value back to the statement that called the method?
What does this line mean?
total = sum(value1, value2);
What does this mean?
return result;
This line return the value in the result variable back to the method that called it.
What is this:
int x = 10, y= 15; double average; average = sum(x, y) / 2.0;
What is this line of code?
customerName = fullName(“John”, “Martin”);
This line calls the fullName method, passing “John” and “Martin” as arguments. The method returns a reference to a String object containing “John Martin”. The reference is assigned to the customerName variable.
What is a method signature?
Method signature – void + method name
What is a method header?
Method header – line before opening and closing curly braces
What is public?
Public – all files have access to it – access specifier (if you use private – only a given file has )
Public static is not a class method
What is void?
Void – non value returning method – runs block of code without returning anything