Java Methods
Objective: To apply predefined and user defined methods
in Java programming.
What is a Method in Java?
• Is a block of code or collection of statements or a set of code
grouped together to perform a certain task or operation.
• Used to achieve a reusability of code. This means that a
method can written once and use it many times.
What is a Method in Java?
• Provides easy modification and readability of code by adding
or removing a chunk of code.
• It is executed only when we call or invoke it.
• The most important method in java is the main() method.
Types of Method
• Predefined
Method
• User-defined
Method
Predefined Method
What is Predefined Method
a Method in Java?
Methods already written Organized as a collection
and provided by Java of classes (class libraries)
Method Type: data type
To use: it needs an
of value returned by
import package
method
Predefined Method
Are the method Also known as
that is already standard library
defined in Java method or built-in
class libraries. method.
Example
of
Predefin
ed
Classes
User-Defined Method
User-Defined Methods
The method This method is
written by the user modified according
or programmer to the requirement
Types of User-Defined Method
Non – value returning method
Value returning method
Methods Overloading
Non - Value Returning Method
• When the method returns nothing,
the return keyword at the end of the
method is optional.
• It uses void keyword.
• Similar in structure to value-returning
methods
Void Method
• Similar in structure to value-returning
methods
• Call to method is always stand-alone
statement
• Can use return statement to exit
method early
Syntax of Creating a method
without a return value
Access Specifier
Return Method
Type Name
public static type name() Method Header
{
// method boy or method signature
}
public class Method {
public static void display()
{
Example of a System.out.println("Hi Java");
User-Defined }
Method public static void main(String[] args) {
// call or invoke the Method
display();
} // end of main
}//end of class
public class Method {
public static void display()
{
System.out.println("Hi Java");
}
public static void main(String[] args) {
// call or invoke the Method
display();
} // end of main
}//end of class
Value Returning Method
• Calculate and return a value
• Used to save value for later calculation or
print value
• Uses a return keyword.
public class Method {
public static int sum(int x, int y)
{
int sum = x + y;
System.out.println("Sum = "+sum);
return sum;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a, b;
System.out.print("Enter value for a:");
a = input.nextInt();
System.out.print("Enter value for b:");
b = input.nextInt();
System.out.println(“The sum is”+sum(a,b));
}
}
Variable x, y are arguments or known as the
actual parameters;
Variable a ,b are known as format parameters;
Naming a Method
• remember that the method name must be a verb and
start with a lowercase letter.
• If the method name has more than two words, the first
name must be a verb followed by adjective or noun.
• In the multi-word method name, the first letter of each
word must be in uppercase except the first word.
Example:
• Single-word method name: sum(), area()
• Multi-word method name: computeCircle(),
calculateArea()
Naming a Method
• It is also possible that a method has the same
name as another method name in the same
class, it is known as method overloading
Primitive Type Wrapper Classes
as Parameters
• If a formal parameter is of the primitive data type and the corresponding actual
parameter is a variable, then the formal parameter cannot change the value of the
actual parameter
• Only reference variables can pass values outside the method (except, of course, for
the return value)
• Corresponding to each primitive data type, Java provides a class so that the values of
primitive data types can be wrapped in objects
• The class Integer does not provide a method to change the value of an existing
Integer object
• The same is true of other wrapper classes
Reference Variables as
Parameters
• If a formal parameter is a reference variable:
• Copies value of corresponding actual parameter
• Value of actual parameter is address of the object where
actual data is stored
• Both formal and actual parameter refer to same object
Use Reference Variables as
Parameters
• Can return more than one value from a method
• Can change the value of the actual object
• When passing address, would save memory space and time,
relative to copying large amount of data
Method Overloading
• Method Overloading: is creating several methods
within a class, with the same name.
• The signature of the method consists of the method
name and its formal parameter list
• Two methods have different signatures if they have
either different names or different format parameter
lists
• Note that the signature of a method does not include the return type of
the method
Method Overloading
- by changing the number of arguments
Method Overloading
- by changing the data types